RomZay commited on
Commit
2827a07
·
verified ·
1 Parent(s): bfc9a54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -45
app.py CHANGED
@@ -11,32 +11,24 @@ headers = {
11
  "Content-Type": "application/json",
12
  }
13
 
14
- def respond(
15
- message,
16
- history: list[tuple[str, str, str, str, str, str]], # Added profile picture fields
17
- system_message,
18
- max_tokens,
19
- top_p,
20
- temperature,
21
- ):
22
  messages = []
23
-
24
- for val in history:
25
- user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic = val
26
-
27
  if user_message:
28
  messages.append({
29
  "role": "user",
30
  "content": user_message,
31
  "profile": user_profile,
32
- "picture": user_pic
33
  })
34
  if assistant_message:
35
  messages.append({
36
  "role": "assistant",
37
  "content": assistant_message,
38
  "profile": assistant_profile,
39
- "picture": "API.png"
40
  })
41
 
42
  data = {
@@ -51,49 +43,53 @@ def respond(
51
  }
52
 
53
  response = requests.post(API_URL, headers=headers, data=json.dumps(data))
54
-
55
  if response.status_code == 200:
56
  response_json = response.json()
57
- print(response_json)
58
- respond = response_json["msq"]["message"][0]
59
- yield respond
60
  else:
61
- response_json = response.json()
62
- yield "Error: " + response_json.get("error", "Unknown error occurred.")
63
 
64
-
65
- """
66
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
67
- """
68
- def custom_render(message, history):
69
- formatted_history = ""
70
  for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
71
  if user_message:
72
- formatted_history += f"<div style='display: flex; align-items: center;'>"
73
  if user_pic:
74
- formatted_history += f"<img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>"
75
- formatted_history += f"<b>{user_profile}:</b> {user_message}</div><br>"
76
 
77
  if assistant_message:
78
- formatted_history += f"<div style='display: flex; align-items: center;'>"
79
  if assistant_pic:
80
- formatted_history += f"<img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>"
81
- formatted_history += f"<b>{assistant_profile}:</b> {assistant_message}</div><br>"
82
 
83
- return formatted_history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
- demo = gr.ChatInterface(
86
- respond,
87
- additional_inputs=[
88
- gr.Textbox(value="You are P-MSQ (Messaging Service Query), a friendly AI Chatbot that can help in any situations. Answer in user's language as concisely as possible.", label="System message"),
89
- gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens"),
90
- gr.Slider(minimum=0, maximum=2, value=0.8, step=0.1, label="Top P"),
91
- gr.Slider(minimum=0.1, maximum=1, value=0.7, step=0.1, label="Temperature"),
92
- gr.File(label="Upload User Profile Picture"),
93
- gr.File(label="Upload Assistant Profile Picture")
94
- ],
95
- chatbot_ui=custom_render
96
- )
97
 
98
  if __name__ == "__main__":
99
  demo.launch()
 
11
  "Content-Type": "application/json",
12
  }
13
 
14
+ ASSISTANT_PIC_PATH = "API.png"
15
+
16
+ def respond(message, history, system_message, max_tokens, top_p, temperature):
 
 
 
 
 
17
  messages = []
18
+ for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
 
 
 
19
  if user_message:
20
  messages.append({
21
  "role": "user",
22
  "content": user_message,
23
  "profile": user_profile,
24
+ "picture": user_pic
25
  })
26
  if assistant_message:
27
  messages.append({
28
  "role": "assistant",
29
  "content": assistant_message,
30
  "profile": assistant_profile,
31
+ "picture": assistant_pic
32
  })
33
 
34
  data = {
 
43
  }
44
 
45
  response = requests.post(API_URL, headers=headers, data=json.dumps(data))
46
+
47
  if response.status_code == 200:
48
  response_json = response.json()
49
+ assistant_reply = response_json["msq"]["message"][0]
50
+ history.append((message, assistant_reply, "User", "Assistant", None, ASSISTANT_PIC_PATH))
51
+ return history, assistant_reply
52
  else:
53
+ return history, "Error: " + response.json().get("error", "Unknown error occurred.")
 
54
 
55
+ def render_message(history):
56
+ messages_html = ""
 
 
 
 
57
  for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
58
  if user_message:
59
+ messages_html += f"<div style='display: flex; align-items: center;'>"
60
  if user_pic:
61
+ messages_html += f"<img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>"
62
+ messages_html += f"<b>{user_profile}:</b> {user_message}</div><br>"
63
 
64
  if assistant_message:
65
+ messages_html += f"<div style='display: flex; align-items: center;'>"
66
  if assistant_pic:
67
+ messages_html += f"<img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>"
68
+ messages_html += f"<b>{assistant_profile}:</b> {assistant_message}</div><br>"
69
 
70
+ return messages_html
71
+
72
+
73
+ with gr.Blocks() as demo:
74
+ chatbot_output = gr.HTML()
75
+ msg_input = gr.Textbox(show_label=False, placeholder="Type your message here...")
76
+
77
+ system_message = gr.Textbox(value="You are P-MSQ (Messaging Service Query), a friendly AI Chatbot that can help in any situations.", label="System message")
78
+ max_tokens = gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens")
79
+ top_p = gr.Slider(minimum=0, maximum=2, value=0.8, step=0.1, label="Top P")
80
+ temperature = gr.Slider(minimum=0.1, maximum=1, value=0.7, step=0.1, label="Temperature")
81
+
82
+ history_state = gr.State([])
83
+ def user_interaction(message, history, system_message, max_tokens, top_p, temperature):
84
+ history, assistant_reply = respond(message, history, system_message, max_tokens, top_p, temperature)
85
+ return render_message(history), history
86
+ msg_input.submit(user_interaction,
87
+ inputs=[msg_input, history_state, system_message, max_tokens, top_p, temperature],
88
+ outputs=[chatbot_output, history_state])
89
 
90
+ gr.Markdown("## Chat Interface with Profile Pictures")
91
+ chatbot_output.render()
92
+ msg_input.render()
 
 
 
 
 
 
 
 
 
93
 
94
  if __name__ == "__main__":
95
  demo.launch()