afif2100 commited on
Commit
56385dc
·
verified ·
1 Parent(s): 896453b

Upload ui_interface.py

Browse files
Files changed (1) hide show
  1. ui_interface.py +50 -0
ui_interface.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+
5
+
6
+ BOT_URL = "http://localhost:1212/api/v1/chat"
7
+
8
+ def llm_response(message, history):
9
+
10
+ # Last Message is : message
11
+ # History is : [{'role': 'user', 'metadata': {'title': None}, 'content': 'hi', 'options': None}, {'role': 'assistant', 'metadata': {'title': None}, 'content': 'I am a bot', 'options': None}]
12
+ # Send the message to the bot and get the response
13
+
14
+ # Create user history
15
+ payload_msg = []
16
+ if len(history) > 0:
17
+ for hist in history:
18
+ hist.pop("metadata", None)
19
+ hist.pop("options", None)
20
+
21
+ payload_msg.append(hist)
22
+
23
+ # Add the user message
24
+ user_message = {"role": "user",
25
+ "content": message}
26
+
27
+ payload_msg.append(user_message)
28
+
29
+ # Send the message to the bot
30
+ payload = {"messages": payload_msg}
31
+
32
+ resp = requests.post(BOT_URL, json=payload)
33
+
34
+ resp_json = resp.json()
35
+ try:
36
+ assistant_message = resp_json["choices"][0]['message']['content']
37
+ except KeyError:
38
+ print(f"Error in response: {resp_json}")
39
+ assistant_message = "Sorry, I did not understand that."
40
+
41
+
42
+ return assistant_message
43
+
44
+ with gr.Blocks(fill_height=True) as bot:
45
+ # placeholder = "<br>Ask Me Anything About Remote Work!"
46
+ # chatbot = gr.Chatbot(placeholder=placeholder)
47
+ gr.ChatInterface(fn=llm_response, type="messages")#, chatbot=chatbot)
48
+
49
+ if __name__ == "__main__":
50
+ bot.launch(share=False)