File size: 1,483 Bytes
a2c48e6
 
30c98aa
660fc7d
30c98aa
a2c48e6
e1b3d3f
e5c2107
 
 
 
 
 
6b277eb
a2c48e6
e1b3d3f
 
a2c48e6
 
 
 
 
 
 
 
e5c2107
6dbd92a
e5c2107
 
 
6dbd92a
e5c2107
6dbd92a
30c98aa
e1b3d3f
 
e5c2107
7875c2c
e1b3d3f
 
a2c48e6
 
b3c5892
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import requests
import json
import gradio as gr
import os

# Define the function to call the API
def translate(text, lang):
    if not lang:
        return "Error: Please select a translation language."
        
    if not text:
        return "Error: Please enter text for translation."

    url = os.getenv("TRANSLATION_URL")
    payload = json.dumps({
        "text": text,
        "lang": lang  # Add language to the payload
    })
    headers = {
        'Content-Type': 'application/json'
    }
    
    # Make the API request
    response = requests.request("POST", url, headers=headers, data=payload)
    
    # Parse the response and handle potential errors
    response_data = response.json()  # Parse the JSON response
    if "errors" in response_data:
        lang_error = response_data["errors"].get("lang", ["An error occurred"])[0]
        return f"Error: {lang_error}"  # Return the error message
    
    translation = response_data.get("translation", "Translation not found")  # Get the translation
    return translation

# Create the Gradio interface with a textbox and dropdown
textbox = gr.Textbox(placeholder="Enter text to translate...")
dropdown = gr.Dropdown(choices=["", "english-twi", "twi-english"], label="Select Translation Language", value="")  # Include a default empty option

# Pass both inputs to the translate function
demo = gr.Interface(fn=translate, inputs=[textbox, dropdown], outputs="text")

# Launch the Gradio app
demo.launch(share=True)