Alekseystr commited on
Commit
c388cd8
verified
1 Parent(s): 2bed002

Create main.py

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