File size: 2,945 Bytes
cb6f452
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a869cd4
cb6f452
 
a869cd4
cb6f452
 
a869cd4
cb6f452
 
 
 
 
 
 
 
 
 
 
 
 
 
a869cd4
 
 
 
cb6f452
 
a869cd4
 
cb6f452
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a869cd4
 
cb6f452
 
a869cd4
 
cb6f452
 
 
a869cd4
cb6f452
 
 
cd12f22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# -*- coding: utf-8 -*-
"""Translation_APP.ipynb

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1EVFldoVPoPgAsak48hRkL_D_jhCo76r_
"""

from transformers import AutoModelForCausalLM, AutoTokenizer
from gtts import gTTS
import torch
import gradio as gr

device = "cuda" if torch.cuda.is_available() else "cpu"

language_model_name = "Qwen/Qwen2-1.5B-Instruct"
language_model = AutoModelForCausalLM.from_pretrained(
    language_model_name,
    torch_dtype="auto",
    device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(language_model_name)

def process_input(input_text, action):
    if action == "Translate to English":
        prompt = f"Please translate the following text into English: {input_text}"
        lang = "en"
    elif action == "Translate to Chinese":
        prompt = f"Please translate the following text into Chinese: {input_text}"
        lang = "zh-cn"
    elif action == "Translate to Japanese":
        prompt = f"Please translate the following text into Japanese: {input_text}"
        lang = "ja"
    else:
        prompt = input_text
        lang = "en"

    messages = [
        {"role": "system", "content": "You are a helpful AI assistant."},
        {"role": "user", "content": prompt}
    ]
    text = tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True
    )
    
    # Encode input with attention mask
    model_inputs = tokenizer([text], return_tensors="pt", padding=True, truncation=True).to(device)
    attention_mask = model_inputs["attention_mask"]

    generated_ids = language_model.generate(
        input_ids=model_inputs.input_ids,
        attention_mask=attention_mask,
        max_new_tokens=512
    )
    generated_ids = [
        output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
    ]

    output_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
    return output_text, lang

def text_to_speech(text, lang):
    tts = gTTS(text=text, lang=lang)
    filename = "output_audio.mp3"
    tts.save(filename)
    return filename

def handle_interaction(input_text, action):
    output_text, lang = process_input(input_text, action)
    audio_filename = text_to_speech(output_text, lang)
    return output_text, audio_filename

action_options = ["Translate to English", "Translate to Chinese", "Translate to Japanese", "Chat"]

iface = gr.Interface(
    fn=handle_interaction,
    inputs=[
        gr.Textbox(label="Input Text"),
        gr.Dropdown(action_options, label="Select Action")
    ],
    outputs=[
        gr.Textbox(label="Output Text"),
        gr.Audio(label="Output Audio")
    ],
    title="Translation and Chat App using AI",
    description="Translate input text or chat based on the selected action.",
    theme="gradio/soft"
)

if __name__ == "__main__":
    iface.launch(share=True)