import requests import json import gradio as gr import os # Define the function to call the API def translate(text, lang): 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 extract the translation response_data = response.json() # Parse the JSON response 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") # Pass both inputs to the translate function demo = gr.Interface(fn=translate, inputs=[textbox, dropdown], outputs="text") # Launch the Gradio app demo.launch()