jaya-sandeep-22 commited on
Commit
e58a6b2
·
verified ·
1 Parent(s): 74862f2

Upload 2 files

Browse files
Files changed (2) hide show
  1. requirements.txt +5 -0
  2. translation_app.py +92 -0
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ transformers
3
+ torch
4
+ gtts
5
+ accelerate
translation_app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Translation_APP.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1EVFldoVPoPgAsak48hRkL_D_jhCo76r_
8
+ """
9
+
10
+ !pip install accelerate gTTS gradio transformers
11
+
12
+ from transformers import AutoModelForCausalLM, AutoTokenizer
13
+ from gtts import gTTS
14
+ import torch
15
+ import gradio as gr
16
+
17
+ device = "cuda" if torch.cuda.is_available() else "cpu"
18
+
19
+ language_model_name = "Qwen/Qwen2-1.5B-Instruct"
20
+ language_model = AutoModelForCausalLM.from_pretrained(
21
+ language_model_name,
22
+ torch_dtype="auto",
23
+ device_map="auto"
24
+ )
25
+ tokenizer = AutoTokenizer.from_pretrained(language_model_name)
26
+
27
+ def process_input(input_text, action):
28
+ if action == "Translate to English":
29
+ prompt = f"Please translate the following text into English:{input_text}"
30
+ lang = "en"
31
+ elif action == "Translate to Chinese":
32
+ prompt = f"Please translate the following text into Chinese:{input_text}"
33
+ lang = "zh-cn"
34
+ elif action == "Translate to Japanese":
35
+ prompt = f"Please translate the following text into Japanese:{input_text}"
36
+ lang = "ja"
37
+ else:
38
+ prompt = input_text
39
+ lang = "en"
40
+
41
+ messages = [
42
+ {"role": "system", "content": "You are a helpful AI assistant."},
43
+ {"role": "user", "content": prompt}
44
+ ]
45
+ text = tokenizer.apply_chat_template(
46
+ messages,
47
+ tokenize=False,
48
+ add_generation_prompt=True
49
+ )
50
+ model_inputs = tokenizer([text], return_tensors="pt").to(device)
51
+
52
+ generated_ids = language_model.generate(
53
+ model_inputs.input_ids,
54
+ max_new_tokens=512
55
+ )
56
+ generated_ids = [
57
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
58
+ ]
59
+
60
+ output_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
61
+ return output_text, lang
62
+
63
+ def text_to_speech(text, lang):
64
+ tts = gTTS(text=text, lang=lang)
65
+ filename = "output_audio.mp3"
66
+ tts.save(filename)
67
+ return filename
68
+
69
+ def handle_interaction(input_text, action):
70
+ output_text, lang = process_input(input_text, action)
71
+ audio_filename = text_to_speech(output_text, lang)
72
+ return output_text, audio_filename
73
+
74
+ action_options = ["Translate to English", "Translate to Chinese", "Translate to Japanese", "Chat"]
75
+
76
+ iface = gr.Interface(
77
+ fn=handle_interaction,
78
+ inputs=[
79
+ gr.Textbox(label="input text"),
80
+ gr.Dropdown(action_options, label="select action")
81
+ ],
82
+ outputs=[
83
+ gr.Textbox(label="output text"),
84
+ gr.Audio(label="output audio")
85
+ ],
86
+ title="Translation and Chat App using AI",
87
+ description="Translate input text or chat based on the selected action.",
88
+ theme= "gradio/soft"
89
+ )
90
+
91
+ if __name__ == "__main__":
92
+ iface.launch(share=True)