Spaces:
Running
Running
File size: 1,785 Bytes
60a6e68 |
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import gradio as gr
import requests
def chat_api(prompt, language, tgt_language):
url = "https://slabstech-dhwani-server-workshop.hf.space/v1/chat"
headers = {
"accept": "application/json",
"Content-Type": "application/json"
}
payload = {
"prompt": prompt,
"src_lang": language,
"tgt_lang": tgt_language
}
try:
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
return f"Error: {str(e)}"
# Create Gradio interface
with gr.Blocks(title="Chat API Interface") as demo:
gr.Markdown("# Chat API Interface")
with gr.Row():
with gr.Column():
# Input components
prompt_input = gr.Textbox(
label="Prompt",
placeholder="Enter your prompt here (e.g., 'hi')"
)
language_input = gr.Textbox(
label="Source Language",
placeholder="Enter source language code (e.g., 'eng_Latn')",
value="eng_Latn"
)
tgt_language_input = gr.Textbox(
label="Target Language",
placeholder="Enter target language code (e.g., 'eng_Latn')",
value="eng_Latn"
)
submit_btn = gr.Button("Submit")
with gr.Column():
# Output component
output = gr.JSON(label="Response")
# Connect the button click to the API function
submit_btn.click(
fn=chat_api,
inputs=[prompt_input, language_input, tgt_language_input],
outputs=output
)
# Launch the interface
demo.launch() |