muzammil-eds commited on
Commit
39ac371
·
verified ·
1 Parent(s): 128a03d

Create app.py

Browse files

New files added

Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from unsloth import FastLanguageModel
3
+
4
+
5
+ model, tokenizer = FastLanguageModel.from_pretrained(
6
+ model_name = "muzammil-eds/Meta-Llama-3.1-8B-Instruct-English-to-French-v2",
7
+ dtype = None,
8
+ load_in_4bit = True,
9
+ )
10
+ FastLanguageModel.for_inference(model)
11
+
12
+ def process_input(model, tokenizer, input_text):
13
+ prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
14
+
15
+ ### Instruction:
16
+ Translate the following English text to French.
17
+
18
+ ### Input:
19
+ {}
20
+
21
+ ### Response:
22
+ """
23
+
24
+ formatted_prompt = prompt.format( input_text)
25
+ inputs = tokenizer([formatted_prompt], return_tensors="pt").to("cuda")
26
+ outputs = model.generate(**inputs, max_new_tokens=512, use_cache=True)
27
+ decoded_output = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
28
+ response_start = "### Response:"
29
+ response = decoded_output.split(response_start)[-1].strip()
30
+
31
+ return response
32
+
33
+
34
+
35
+
36
+ # Define the Gradio interface
37
+ def gradio_app(input_text):
38
+ output = process_input(model, tokenizer, input_text)
39
+ return output
40
+
41
+ # Create the Gradio interface
42
+ interface = gr.Interface(
43
+ fn=gradio_app,
44
+ inputs=gr.Textbox(label="Enter your input text"),
45
+ outputs=gr.Textbox(label="Generated Output"),
46
+ title="Text to Response Generator",
47
+ description="Enter input text and get a response."
48
+ )
49
+
50
+ interface.launch()