Spaces:
Runtime error
Runtime error
update ui
Browse files
app.py
CHANGED
|
@@ -1,14 +1,88 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
def generate_response(user_input):
|
| 5 |
-
response = requests.post(
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
outputs=gr.Textbox()
|
| 12 |
-
)
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import json
|
| 4 |
|
| 5 |
def generate_response(user_input):
|
| 6 |
+
response = requests.post(
|
| 7 |
+
url="https://llm01.mosaik.io/chat",
|
| 8 |
+
headers={'secret-key': 'ABCDEF'},
|
| 9 |
+
json={"uid": 123, "message": user_input, "stream": False}
|
| 10 |
+
)
|
| 11 |
+
print(response.content)
|
| 12 |
|
| 13 |
+
try:
|
| 14 |
+
response_json = response.json()
|
| 15 |
+
response_json = json.loads(response_json)
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
if "intent" in response_json or "initial_request" in response_json:
|
| 18 |
+
|
| 19 |
+
param = response_json.get('initial_request')
|
| 20 |
+
intent = response_json.get("intent")
|
| 21 |
+
|
| 22 |
+
response_content = (intent,param)
|
| 23 |
+
print(response_content)
|
| 24 |
+
else:
|
| 25 |
+
response_content = (response_json,)
|
| 26 |
+
|
| 27 |
+
except json.JSONDecodeError:
|
| 28 |
+
response_content = (response_json,)
|
| 29 |
+
|
| 30 |
+
return json.dumps(response_content)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def push_to_db(response, user_input, correct_intent, correct_params):
|
| 34 |
+
response_list = json.loads(response)
|
| 35 |
+
|
| 36 |
+
print(len(response_list))
|
| 37 |
+
|
| 38 |
+
if len(response_list)>1:
|
| 39 |
+
|
| 40 |
+
db_entry = {
|
| 41 |
+
"query":user_input,
|
| 42 |
+
"predicted_intent":response_list[0],
|
| 43 |
+
"predicted_parameter":response_list[1],
|
| 44 |
+
"corrected_intent":correct_intent,
|
| 45 |
+
"corrected_parameter":correct_params
|
| 46 |
+
|
| 47 |
+
}
|
| 48 |
+
else:
|
| 49 |
+
db_entry = {
|
| 50 |
+
"query":user_input,
|
| 51 |
+
"predicted_intent":"",
|
| 52 |
+
"predicted_parameter":"",
|
| 53 |
+
"corrected_intent":correct_intent,
|
| 54 |
+
"corrected_parameter":correct_params
|
| 55 |
+
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
response = requests.post(
|
| 59 |
+
url="https://llm01.mosaik.io/add_feedback",
|
| 60 |
+
headers={'secret-key': 'ABCDEF'},
|
| 61 |
+
json=db_entry
|
| 62 |
+
)
|
| 63 |
+
print("Data pushed to database:", db_entry)
|
| 64 |
+
return "Data pushed to database successfully!"
|
| 65 |
+
|
| 66 |
+
with gr.Blocks() as demo:
|
| 67 |
+
user_input = gr.Textbox(lines=2, placeholder="Enter your text here...",label="Query")
|
| 68 |
+
corret_intent = gr.Textbox(lines=2, placeholder="Correct Intent",label="Correct Intent")
|
| 69 |
+
correct_params = gr.Textbox(lines=2, placeholder="Correct Parameters",label="Correct Parameters")
|
| 70 |
+
output = gr.Textbox()
|
| 71 |
+
|
| 72 |
+
generate_btn = gr.Button("Generate Response")
|
| 73 |
+
push_btn = gr.Button("Hit")
|
| 74 |
+
|
| 75 |
+
generate_btn.click(
|
| 76 |
+
generate_response,
|
| 77 |
+
inputs=[user_input],
|
| 78 |
+
outputs=[output]
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
push_btn.click(
|
| 83 |
+
push_to_db,
|
| 84 |
+
inputs=[output, user_input, corret_intent, correct_params],
|
| 85 |
+
outputs=[output]
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
demo.launch()
|