Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,41 +4,39 @@ import os
|
|
4 |
|
5 |
# Retrieve secrets
|
6 |
API_URL = os.environ.get("API_URL")
|
7 |
-
HF_TOKEN = os.environ.get("HF_TOKEN")
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
}
|
15 |
|
16 |
def query(payload):
|
17 |
response = requests.post(API_URL, headers=headers, json=payload)
|
18 |
return response.json()
|
19 |
|
20 |
-
def translate(text,
|
21 |
-
|
22 |
-
|
23 |
|
24 |
instruction = f"Translate the text from {source_lang} to {target_lang}"
|
25 |
|
26 |
input_text = f"""### Instruction: {instruction}
|
|
|
27 |
### Input: {text}
|
|
|
28 |
### Response:
|
29 |
"""
|
30 |
|
31 |
-
# Generate the translation using the Hugging Face Inference API
|
32 |
output = query({
|
33 |
"inputs": input_text,
|
34 |
"parameters": {
|
35 |
-
"return_text": False,
|
36 |
"return_full_text": False
|
37 |
}
|
38 |
})
|
39 |
|
40 |
-
# Extract the translated text from the
|
41 |
-
translated_text = output[0]['generated_text']
|
42 |
|
43 |
return translated_text
|
44 |
|
@@ -47,14 +45,15 @@ iface = gr.Interface(
|
|
47 |
fn=translate,
|
48 |
inputs=[
|
49 |
gr.Textbox(label="Enter text to translate"),
|
50 |
-
gr.
|
|
|
51 |
],
|
52 |
outputs=gr.Textbox(label="Translated Text"),
|
53 |
title="English-Welsh Translator",
|
54 |
-
description="Translate text between English and Welsh using a
|
55 |
examples=[
|
56 |
-
["Hello, how are you?",
|
57 |
-
["Bore da!",
|
58 |
],
|
59 |
)
|
60 |
|
|
|
4 |
|
5 |
# Retrieve secrets
|
6 |
API_URL = os.environ.get("API_URL")
|
|
|
7 |
|
8 |
+
import gradio as gr
|
9 |
+
import requests
|
10 |
+
import os
|
11 |
+
|
12 |
+
headers = {"Authorization": f"Bearer {os.environ['HF_TOKEN']}"}
|
|
|
13 |
|
14 |
def query(payload):
|
15 |
response = requests.post(API_URL, headers=headers, json=payload)
|
16 |
return response.json()
|
17 |
|
18 |
+
def translate(text, source_lang, target_lang):
|
19 |
+
if source_lang == target_lang:
|
20 |
+
return text
|
21 |
|
22 |
instruction = f"Translate the text from {source_lang} to {target_lang}"
|
23 |
|
24 |
input_text = f"""### Instruction: {instruction}
|
25 |
+
|
26 |
### Input: {text}
|
27 |
+
|
28 |
### Response:
|
29 |
"""
|
30 |
|
|
|
31 |
output = query({
|
32 |
"inputs": input_text,
|
33 |
"parameters": {
|
|
|
34 |
"return_full_text": False
|
35 |
}
|
36 |
})
|
37 |
|
38 |
+
# Extract the translated text from the model output
|
39 |
+
translated_text = output[0]['generated_text'].strip()
|
40 |
|
41 |
return translated_text
|
42 |
|
|
|
45 |
fn=translate,
|
46 |
inputs=[
|
47 |
gr.Textbox(label="Enter text to translate"),
|
48 |
+
gr.Radio(["English", "Welsh"], label="Source Language", value="English"),
|
49 |
+
gr.Radio(["English", "Welsh"], label="Target Language", value="Welsh"),
|
50 |
],
|
51 |
outputs=gr.Textbox(label="Translated Text"),
|
52 |
title="English-Welsh Translator",
|
53 |
+
description="Translate text between English and Welsh using a Hugging Face Inference Endpoint.",
|
54 |
examples=[
|
55 |
+
["Hello, how are you?", "English", "Welsh"],
|
56 |
+
["Bore da!", "Welsh", "English"],
|
57 |
],
|
58 |
)
|
59 |
|