Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,39 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
|
4 |
-
# Load the
|
5 |
-
model_name = '
|
6 |
-
model =
|
7 |
-
tokenizer =
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
12 |
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
13 |
return translated_text
|
14 |
|
15 |
-
def chat(message):
|
16 |
-
translated_message = translate_text(message)
|
17 |
return translated_message
|
18 |
|
|
|
|
|
|
|
19 |
# Create the Gradio interface
|
20 |
-
interface = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
interface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
|
4 |
+
# Load the Mistral model and tokenizer
|
5 |
+
model_name = 'mistral/mistral-7b' # Replace with your specific Mistral model
|
6 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
|
9 |
+
# Define a function to handle translation
|
10 |
+
def translate_text(text, src_lang, tgt_lang):
|
11 |
+
# Prepare the input text for the model
|
12 |
+
# For simplicity, we'll just append source and target languages to the input text
|
13 |
+
input_text = f"{src_lang} to {tgt_lang}: {text}"
|
14 |
+
inputs = tokenizer(input_text, return_tensors="pt")
|
15 |
+
translated = model.generate(**inputs)
|
16 |
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
17 |
return translated_text
|
18 |
|
19 |
+
def chat(message, src_lang, tgt_lang):
|
20 |
+
translated_message = translate_text(message, src_lang, tgt_lang)
|
21 |
return translated_message
|
22 |
|
23 |
+
# Define the language options
|
24 |
+
languages = ["English", "French", "German", "Spanish", "Chinese"] # Extend this list as needed
|
25 |
+
|
26 |
# Create the Gradio interface
|
27 |
+
interface = gr.Interface(
|
28 |
+
fn=chat,
|
29 |
+
inputs=[
|
30 |
+
gr.inputs.Textbox(label="Enter text"),
|
31 |
+
gr.inputs.Dropdown(choices=languages, label="Source Language"),
|
32 |
+
gr.inputs.Dropdown(choices=languages, label="Target Language")
|
33 |
+
],
|
34 |
+
outputs="text",
|
35 |
+
title="Mistral Translation Chatbot",
|
36 |
+
description="Translate text between different languages using the Mistral model."
|
37 |
+
)
|
38 |
|
39 |
interface.launch()
|