David commited on
Commit
be52360
·
1 Parent(s): 59b000d
Files changed (1) hide show
  1. app.py +30 -1
app.py CHANGED
@@ -2,6 +2,8 @@ import gradio as gr
2
  import requests
3
  import os
4
  import json
 
 
5
 
6
  # Set up the API endpoint and key
7
  API_URL = os.getenv("RUNPOD_API_URL")
@@ -12,6 +14,29 @@ headers = {
12
  "Content-Type": "application/json"
13
  }
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def respond(message, history, system_message, max_tokens, temperature, top_p):
16
  if system_message is not None:
17
  messages = [{"role": "system", "content": system_message}]
@@ -47,7 +72,10 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
47
  print(f"Formatted API Response: {json.dumps(response_json, indent=2)}")
48
 
49
  if 'choices' in response_json and len(response_json['choices']) > 0:
50
- return response_json['choices'][0]['message']['content']
 
 
 
51
  else:
52
  return f"Error: Unexpected response format. Full response: {response_json}"
53
 
@@ -72,6 +100,7 @@ demo = gr.ChatInterface(
72
  gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
73
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
74
  ],
 
75
  )
76
 
77
  if __name__ == "__main__":
 
2
  import requests
3
  import os
4
  import json
5
+ import re
6
+ import html
7
 
8
  # Set up the API endpoint and key
9
  API_URL = os.getenv("RUNPOD_API_URL")
 
14
  "Content-Type": "application/json"
15
  }
16
 
17
+ def escape_html(text):
18
+ return html.escape(text)
19
+
20
+ def format_response_for_display(text):
21
+ # Escape HTML entities
22
+ text = escape_html(text)
23
+
24
+ # Format <thinking> and <reflection> tags (case-insensitive)
25
+ text = re.sub(r'(?i)(<thinking>)(.*?)(</thinking>)',
26
+ r'<span style="font-family: monospace; color: blue;">\1</span><i>\2</i><span style="font-family: monospace; color: blue;">\3</span>',
27
+ text, flags=re.DOTALL)
28
+ text = re.sub(r'(?i)(<reflection>)(.*?)(</reflection>)',
29
+ r'<span style="font-family: monospace; color: green;">\1</span><i>\2</i><span style="font-family: monospace; color: green;">\3</span>',
30
+ text, flags=re.DOTALL)
31
+
32
+ # Remove <output> tags but keep content
33
+ text = re.sub(r'(?i)<output>(.*?)</output>', r'\1', text, flags=re.DOTALL)
34
+
35
+ # Replace newlines with <br> tags
36
+ text = text.replace('\n', '<br>')
37
+
38
+ return text
39
+
40
  def respond(message, history, system_message, max_tokens, temperature, top_p):
41
  if system_message is not None:
42
  messages = [{"role": "system", "content": system_message}]
 
72
  print(f"Formatted API Response: {json.dumps(response_json, indent=2)}")
73
 
74
  if 'choices' in response_json and len(response_json['choices']) > 0:
75
+ content = response_json['choices'][0]['message']['content']
76
+ formatted_content = format_response_for_display(content)
77
+ print(f"Formatted content for display: {formatted_content}") # For debugging
78
+ return formatted_content
79
  else:
80
  return f"Error: Unexpected response format. Full response: {response_json}"
81
 
 
100
  gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
101
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
102
  ],
103
+ css=".message-wrap { white-space: pre-wrap; }"
104
  )
105
 
106
  if __name__ == "__main__":