RomZay commited on
Commit
bfc9a54
·
verified ·
1 Parent(s): 4731fd0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -9
app.py CHANGED
@@ -4,7 +4,7 @@ import json
4
  import os
5
 
6
  API_URL = "https://host.palple.polrambora.com/pmsq"
7
- API_TOKEN = os.getenv("POLLY")
8
 
9
  headers = {
10
  "Authorization": f"{API_TOKEN}",
@@ -13,7 +13,7 @@ headers = {
13
 
14
  def respond(
15
  message,
16
- history: list[tuple[str, str]],
17
  system_message,
18
  max_tokens,
19
  top_p,
@@ -22,11 +22,22 @@ def respond(
22
  messages = []
23
 
24
  for val in history:
25
- if val[0]:
26
- messages.append({"role": "user", "content": val[0]})
27
- if val[1]:
28
- messages.append({"role": "assistant", "content": val[1]})
29
-
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  data = {
32
  "preferences": {
@@ -40,19 +51,37 @@ def respond(
40
  }
41
 
42
  response = requests.post(API_URL, headers=headers, data=json.dumps(data))
43
- print(response)
44
- response_json = response.json()
45
 
46
  if response.status_code == 200:
 
47
  print(response_json)
48
  respond = response_json["msq"]["message"][0]
49
  yield respond
50
  else:
 
51
  yield "Error: " + response_json.get("error", "Unknown error occurred.")
52
 
 
53
  """
54
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
55
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  demo = gr.ChatInterface(
57
  respond,
58
  additional_inputs=[
@@ -60,7 +89,10 @@ demo = gr.ChatInterface(
60
  gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens"),
61
  gr.Slider(minimum=0, maximum=2, value=0.8, step=0.1, label="Top P"),
62
  gr.Slider(minimum=0.1, maximum=1, value=0.7, step=0.1, label="Temperature"),
 
 
63
  ],
 
64
  )
65
 
66
  if __name__ == "__main__":
 
4
  import os
5
 
6
  API_URL = "https://host.palple.polrambora.com/pmsq"
7
+ API_TOKEN = os.getenv("POLLY")
8
 
9
  headers = {
10
  "Authorization": f"{API_TOKEN}",
 
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,
 
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 = {
43
  "preferences": {
 
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=[
 
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__":