surajtiwari commited on
Commit
37bd27d
1 Parent(s): 90a00c9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
+
4
+ # Load the model and tokenizer
5
+ model_name = "SnypzZz/Llama2-13b-Language-translate"
6
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+
9
+ # Function to handle translation
10
+ def translate_text(text, target_language):
11
+ # Tokenize the input text
12
+ inputs = tokenizer.encode(text, return_tensors="pt")
13
+
14
+ # Perform translation
15
+ outputs = model.generate(inputs)
16
+
17
+ # Decode the translated output
18
+ translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
19
+ return translated_text
20
+
21
+ # Gradio interface
22
+ def gradio_app():
23
+ # Create a Gradio interface
24
+ iface = gr.Interface(
25
+ fn=translate_text,
26
+ inputs=[
27
+ gr.inputs.Textbox(label="Input Text"),
28
+ gr.inputs.Dropdown(choices=["fr", "es", "de", "zh"], label="Target Language") # Modify languages accordingly
29
+ ],
30
+ outputs=gr.outputs.Textbox(label="Translated Text"),
31
+ title="Language Translator",
32
+ description="A translation app using SnypzZz/Llama2-13b-Language-translate"
33
+ )
34
+
35
+ # Launch the interface
36
+ iface.launch()
37
+
38
+ if __name__ == "__main__":
39
+ gradio_app()