BounharAbdelaziz commited on
Commit
a8f0c36
β€’
1 Parent(s): b8f49d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -39
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
- HF_TOKEN = os.environ['HF_TOKEN']
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
- if model_pipeline is None:
26
- return "Model failed to load, please refresh the webpage."
27
-
28
- while True:
29
- try:
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
- fn=respond,
41
- inputs="text",
42
- outputs="text",
43
- title="Terjman-Large πŸ‘¨β€πŸ’»πŸ₯°",
44
- description="Translate English text to Moroccan Darija using our Large model (240M) πŸ€—"
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()