Spaces:
Sleeping
Sleeping
AstraBert
commited on
Commit
•
62b6bd5
1
Parent(s):
a142d03
first commit
Browse files- app.py +21 -0
- load_model.py +6 -0
- requirements.txt +3 -0
- utils.py +15 -0
app.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from load_model import pipe
|
3 |
+
from utils import Translation
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
def reply(message, history):
|
8 |
+
txt = Translation(message, "en")
|
9 |
+
if txt.original == "en":
|
10 |
+
response = pipe(message)
|
11 |
+
return response[0]["generated_text"].split("Answer\n")[1]
|
12 |
+
else:
|
13 |
+
translation = txt.translatef()
|
14 |
+
response = pipe(translation)
|
15 |
+
t = Translation(response[0]["generated_text"].split("Answer\n")[1], txt.original)
|
16 |
+
res = t.translatef()
|
17 |
+
return res
|
18 |
+
|
19 |
+
|
20 |
+
demo = gr.ChatInterface(fn=reply, examples=["What is data science?", "你好你好吗?", "Ποιος ήταν ο Αρχιμήδης;", "महात्मा गांधी के बारे में बात करें", "Descrivi la miglior ricetta italiana", "Wo liegt die Mauer von Berlin?"], title="Multilingual-Phi Bot")
|
21 |
+
demo.launch()
|
load_model.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
2 |
+
|
3 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)
|
4 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)
|
5 |
+
|
6 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=2048)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
transformers==4.39.3
|
2 |
+
langdetect==1.0.9
|
3 |
+
deep-translator==1.11.4
|
utils.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langdetect import detect
|
2 |
+
from deep_translator import GoogleTranslator
|
3 |
+
|
4 |
+
class Translation:
|
5 |
+
def __init__(self, text, destination):
|
6 |
+
self.text = text
|
7 |
+
self.destination = destination
|
8 |
+
try:
|
9 |
+
self.original = detect(self.text)
|
10 |
+
except Exception as e:
|
11 |
+
self.original = "auto"
|
12 |
+
def translatef(self):
|
13 |
+
translator = GoogleTranslator(source=self.original, target=self.destination)
|
14 |
+
translation = translator.translate(self.text)
|
15 |
+
return translation
|