Spaces:
Runtime error
Runtime error
BounharAbdelaziz
commited on
Commit
β’
a8f0c36
1
Parent(s):
b8f49d8
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,22 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
|
3 |
-
import time
|
4 |
-
import os
|
5 |
|
6 |
-
|
7 |
-
headers = {"Authorization": "Bearer "+ HF_TOKEN}
|
8 |
|
9 |
-
# Define the model and tokenizer loading
|
10 |
-
def load_model_and_tokenizer():
|
11 |
-
try:
|
12 |
-
tokenizer = AutoTokenizer.from_pretrained("atlasia/Terjman-Large")
|
13 |
-
model = AutoModelForSeq2SeqLM.from_pretrained("atlasia/Terjman-Large")
|
14 |
-
translation_pipeline = pipeline("translation", model=model, tokenizer=tokenizer)
|
15 |
-
return translation_pipeline
|
16 |
-
except Exception as e:
|
17 |
-
print(f"Error loading model and tokenizer: {e}")
|
18 |
-
return None
|
19 |
-
|
20 |
-
# Load the model and tokenizer once at startup
|
21 |
-
model_pipeline = load_model_and_tokenizer()
|
22 |
-
|
23 |
-
# Define the response function
|
24 |
def respond(english_text):
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
result = model_pipeline(english_text)
|
31 |
-
return result[0]['translation_text']
|
32 |
-
except Exception as e:
|
33 |
-
if "estimated_time" in str(e):
|
34 |
-
time.sleep(5) # Wait for 5 seconds before retrying
|
35 |
-
else:
|
36 |
-
return f"An error occurred: {e}"
|
37 |
|
38 |
# Create the Gradio interface
|
39 |
-
app = gr.Interface(
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
)
|
46 |
|
47 |
if __name__ == "__main__":
|
48 |
app.launch()
|
|
|
1 |
+
from gradio_client import Client
|
2 |
import gradio as gr
|
|
|
|
|
|
|
3 |
|
4 |
+
client = Client("https://15fdee77fc28978c9b.gradio.live/")
|
|
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
def respond(english_text):
|
7 |
+
darija_translated_text = client.predict(
|
8 |
+
text=english_text,
|
9 |
+
api_name="/predict"
|
10 |
+
)
|
11 |
+
return darija_translated_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Create the Gradio interface
|
14 |
+
app = gr.Interface(fn=respond,
|
15 |
+
inputs="text",
|
16 |
+
outputs="text",
|
17 |
+
title="Terjman-Supreme π¨βπ»π€―",
|
18 |
+
description="Translate English text to Moroccan Darija using our top and biggest model (3.3B) π€")
|
19 |
+
|
|
|
20 |
|
21 |
if __name__ == "__main__":
|
22 |
app.launch()
|