Guhanselvam commited on
Commit
33a9a5f
·
verified ·
1 Parent(s): 8c7feb4

Create gradio_interface.py

Browse files
Files changed (1) hide show
  1. gradio_interface.py +38 -0
gradio_interface.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+
4
+ # Define the FastAPI API URL
5
+ API_URL = "http://127.0.0.1:8000/llm_api" # Ensure this matches your FastAPI server
6
+
7
+ def query_api(question):
8
+ """
9
+ Function to send a question to the FastAPI backend and return the response.
10
+ """
11
+ try:
12
+ response = requests.post(API_URL, json={"question": question})
13
+
14
+ # Check response status and return the appropriate response
15
+ if response.status_code == 200:
16
+ return response.json().get("response", "No response from the model.")
17
+ else:
18
+ return f"Error: Unable to fetch response. Status Code: {response.status_code}"
19
+ except requests.exceptions.RequestException as e:
20
+ return f"Request failed: {e}"
21
+
22
+ # Create the Gradio interface
23
+ iface = gr.Interface(
24
+ fn=query_api,
25
+ inputs=gr.Textbox(label="Ask a Question"),
26
+ outputs="text",
27
+ title="Chatbot Interface",
28
+ description="Ask any question and get responses from the Gemini model."
29
+ )
30
+
31
+ # Launch the Gradio interface with appropriate handling
32
+ if __name__ == "__main__":
33
+ try:
34
+ iface.launch()
35
+ except KeyboardInterrupt:
36
+ print("Gradio interface stopped manually.")
37
+ except Exception as e:
38
+ print(f"An error occurred: {e}")