Spaces:
Sleeping
Sleeping
Commit
·
88cef52
1
Parent(s):
3797282
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,37 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
import gradio as gr
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
# Load environment variables from .env file
|
| 7 |
+
load_dotenv()
|
| 8 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
|
| 10 |
+
model_name = "Helsinki-NLP/opus-mt-en-fr"
|
| 11 |
+
API_URL = f"https://api-inference.huggingface.co/models/{model_name}"
|
| 12 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 13 |
|
| 14 |
|
| 15 |
+
def query(payload):
|
| 16 |
+
# HTTP POST Request
|
| 17 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 18 |
+
return response.json()
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def translate(input_text):
|
| 22 |
+
# API Request:
|
| 23 |
+
response = query({"inputs": input_text, "options": {"wait_for_model": True}})
|
| 24 |
+
|
| 25 |
+
translation = response[0]["translation_text"]
|
| 26 |
+
|
| 27 |
+
return translation
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
translator = gr.Interface(
|
| 31 |
+
fn=translate,
|
| 32 |
+
inputs=[gr.Textbox(label="Input Text", placeholder="Input Text To Be Translated")],
|
| 33 |
+
outputs=gr.Textbox(label="Translation"),
|
| 34 |
+
title="tTranslatorR-Opus",
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
translator.launch()
|