File size: 985 Bytes
cf2d2cd
89f5567
306dab4
 
cf2d2cd
ccbf7bd
89f5567
 
cf2d2cd
89f5567
51badc8
89f5567
 
51badc8
 
 
 
 
 
 
 
 
 
cf2d2cd
51badc8
cf2d2cd
 
89f5567
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
import gradio as gr
import os
import requests
import time

API_URL = "https://api-inference.huggingface.co/models/atlasia/Terjman-Large-v2"
HF_TOKEN = os.environ['HF_TOKEN']
headers = {"Authorization": "Bearer "+ HF_TOKEN}

def respond(english_text):
    payload = {
        "inputs": english_text,
    }
    
    while True:
        response = requests.post(API_URL, headers=headers, json=payload).json()
        
        if isinstance(response, list) and 'generated_text' in response[0]:
            return response[0]['generated_text']
        elif 'estimated_time' in response:
            time.sleep(5)  # Wait for 5 seconds before retrying
        else:
            return "An error occurred, please refresh the webpage: " + str(response)

app = gr.Interface(fn=respond, inputs="text", outputs="text", title="Terjman-Large-v2 👨‍💻🥰", description="Translate English text to Moroccan Darija using our Large model (240M) 🤗")

if __name__ == "__main__":
    app.launch()