sachin commited on
Commit
60a6e68
·
1 Parent(s): 28e6a38

add-chat api

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ def chat_api(prompt, language, tgt_language):
5
+ url = "https://slabstech-dhwani-server-workshop.hf.space/v1/chat"
6
+
7
+ headers = {
8
+ "accept": "application/json",
9
+ "Content-Type": "application/json"
10
+ }
11
+
12
+ payload = {
13
+ "prompt": prompt,
14
+ "src_lang": language,
15
+ "tgt_lang": tgt_language
16
+ }
17
+
18
+ try:
19
+ response = requests.post(url, headers=headers, json=payload)
20
+ response.raise_for_status()
21
+ return response.json()
22
+ except requests.exceptions.RequestException as e:
23
+ return f"Error: {str(e)}"
24
+
25
+ # Create Gradio interface
26
+ with gr.Blocks(title="Chat API Interface") as demo:
27
+ gr.Markdown("# Chat API Interface")
28
+
29
+ with gr.Row():
30
+ with gr.Column():
31
+ # Input components
32
+ prompt_input = gr.Textbox(
33
+ label="Prompt",
34
+ placeholder="Enter your prompt here (e.g., 'hi')"
35
+ )
36
+ language_input = gr.Textbox(
37
+ label="Source Language",
38
+ placeholder="Enter source language code (e.g., 'eng_Latn')",
39
+ value="eng_Latn"
40
+ )
41
+ tgt_language_input = gr.Textbox(
42
+ label="Target Language",
43
+ placeholder="Enter target language code (e.g., 'eng_Latn')",
44
+ value="eng_Latn"
45
+ )
46
+
47
+ submit_btn = gr.Button("Submit")
48
+
49
+ with gr.Column():
50
+ # Output component
51
+ output = gr.JSON(label="Response")
52
+
53
+ # Connect the button click to the API function
54
+ submit_btn.click(
55
+ fn=chat_api,
56
+ inputs=[prompt_input, language_input, tgt_language_input],
57
+ outputs=output
58
+ )
59
+
60
+ # Launch the interface
61
+ demo.launch()