Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
|
|
3 |
from openai import OpenAI
|
4 |
import json
|
5 |
import requests
|
|
|
6 |
|
7 |
openai_api_key = os.getenv("OPENROUTER_API_KEY")
|
8 |
openai_base_url = os.getenv("OPENAI_BASE_URL")
|
@@ -82,12 +83,12 @@ def think(request):
|
|
82 |
#messages = [{"role": "user", "content": content}]
|
83 |
|
84 |
#if reasoning != "":
|
85 |
-
# messages.append({"role": "assistant", "content": "
|
86 |
payload = {
|
87 |
"model": model,
|
88 |
"messages": messages,
|
89 |
"include_reasoning": include_reasoning,
|
90 |
-
"
|
91 |
}
|
92 |
|
93 |
return requests.post(url, headers=headers, data=json.dumps(payload))
|
@@ -111,40 +112,7 @@ def chat_with_openai(message: str, history: list, temperature: float, max_new_to
|
|
111 |
"""
|
112 |
Call the OpenAI ChatCompletion endpoint using the new client and yield streaming responses.
|
113 |
|
114 |
-
|
115 |
-
|
116 |
-
Args:
|
117 |
-
message (str): The latest user message.
|
118 |
-
history (list): Conversation history as a list of (user, assistant) tuples.
|
119 |
-
temperature (float): Sampling temperature.
|
120 |
-
max_new_tokens (int): Maximum tokens to generate.
|
121 |
-
|
122 |
-
Yields:
|
123 |
-
str: Partial cumulative output from the assistant.
|
124 |
-
"""
|
125 |
-
|
126 |
-
conversation = []
|
127 |
-
|
128 |
-
if (not history and message.startswith("Start a talk therapy session with me.")) or \
|
129 |
-
any(user_msg.startswith("Start a talk therapy session with me.") for user_msg, _ in history):
|
130 |
-
fast_mode = True
|
131 |
-
|
132 |
-
if not history:
|
133 |
-
# Initialize with system prompt and assistant confirmation.
|
134 |
-
conversation.append({"role": "system", "content": SYSTEM_PROMPT})
|
135 |
-
conversation.append({"role": "assistant", "content": "Understood! I will act as the user's healthcare provider..."})
|
136 |
-
|
137 |
-
for user_msg, assistant_msg in history:
|
138 |
-
conversation.append({"role": "user", "content": user_msg})
|
139 |
-
conversation.append({"role": "assistant", "content": assistant_msg})
|
140 |
-
|
141 |
-
conversation.append({"role": "user", "content": message})
|
142 |
-
|
143 |
-
if not fast_mode:
|
144 |
-
# Indicate that the assistant is thinking.
|
145 |
-
yield "HealthAssistant is Thinking! Please wait, your response will output shortly. This may take 10-30 seconds...\n\n"
|
146 |
-
think_result = think(conversation)
|
147 |
-
conversation.append({"role": "assistant", "content": "<think>\n" + think_result + "\n</think> I will now respond to the user's message:\n\n"})
|
148 |
else:
|
149 |
yield "HealthAssistant is Thinking! Please wait, your response will output shortly...\n\n"
|
150 |
|
@@ -203,7 +171,54 @@ def chat_with_openai(message: str, history: list, temperature: float, max_new_to
|
|
203 |
modified_full_response = apply_replacements(full_response)
|
204 |
history.append((message, modified_full_response))
|
205 |
|
206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
|
208 |
# Create the Chatbot component.
|
209 |
chatbot = gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='HealthAssistant')
|
@@ -215,7 +230,7 @@ with gr.Blocks(css=css) as demo:
|
|
215 |
# Add the checkbox directly to the layout
|
216 |
fast_mode_checkbox = gr.Checkbox(label="Fast Mode (Skips Reasoning. Provides Immediate, Less Accurate Responses.) RECOMMENDED FOR TALK THERAPY.", value=False)
|
217 |
|
218 |
-
gr.ChatInterface(
|
219 |
fn=chat_with_openai,
|
220 |
chatbot=chatbot,
|
221 |
fill_height=True,
|
@@ -233,6 +248,15 @@ with gr.Blocks(css=css) as demo:
|
|
233 |
],
|
234 |
cache_examples=False,
|
235 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
236 |
|
237 |
gr.Markdown(LICENSE)
|
238 |
|
|
|
3 |
from openai import OpenAI
|
4 |
import json
|
5 |
import requests
|
6 |
+
import datetime
|
7 |
|
8 |
openai_api_key = os.getenv("OPENROUTER_API_KEY")
|
9 |
openai_base_url = os.getenv("OPENAI_BASE_URL")
|
|
|
83 |
#messages = [{"role": "user", "content": content}]
|
84 |
|
85 |
#if reasoning != "":
|
86 |
+
# messages.append({"role": "assistant", "content": "\n"})
|
87 |
payload = {
|
88 |
"model": model,
|
89 |
"messages": messages,
|
90 |
"include_reasoning": include_reasoning,
|
91 |
+
""
|
92 |
}
|
93 |
|
94 |
return requests.post(url, headers=headers, data=json.dumps(payload))
|
|
|
112 |
"""
|
113 |
Call the OpenAI ChatCompletion endpoint using the new client and yield streaming responses.
|
114 |
|
115 |
+
Implementscontent": "I will now respond to the user's message:\n\n"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
else:
|
117 |
yield "HealthAssistant is Thinking! Please wait, your response will output shortly...\n\n"
|
118 |
|
|
|
171 |
modified_full_response = apply_replacements(full_response)
|
172 |
history.append((message, modified_full_response))
|
173 |
|
174 |
+
def export_chat(history):
|
175 |
+
"""Export chat history as a JSONL file in OpenAI messages format."""
|
176 |
+
if not history:
|
177 |
+
return None
|
178 |
+
|
179 |
+
messages = []
|
180 |
+
# Add a blank system message (to maintain format compatibility, but without revealing the actual system prompt)
|
181 |
+
messages.append({"role": "system", "content": ""})
|
182 |
+
|
183 |
+
# Convert history to messages format
|
184 |
+
for user_msg, assistant_msg in history:
|
185 |
+
messages.append({"role": "user", "content": user_msg})
|
186 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
187 |
+
|
188 |
+
# Convert to JSONL format (each line is a JSON object)
|
189 |
+
jsonl_content = "\n".join([json.dumps(msg) for msg in messages])
|
190 |
+
|
191 |
+
# Create a file for download
|
192 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
193 |
+
file_name = f"chat_export_{timestamp}.jsonl"
|
194 |
+
|
195 |
+
return (file_name, jsonl_content)
|
196 |
+
|
197 |
+
def import_chat(file):
|
198 |
+
"""Import chat history from a JSONL file."""
|
199 |
+
try:
|
200 |
+
content = file.decode('utf-8')
|
201 |
+
lines = [line.strip() for line in content.split('\n') if line.strip()]
|
202 |
+
|
203 |
+
messages = [json.loads(line) for line in lines]
|
204 |
+
|
205 |
+
new_history = []
|
206 |
+
i = 0
|
207 |
+
|
208 |
+
# Skip system message if it's the first one (we don't use imported system prompts)
|
209 |
+
if messages and messages[0]["role"] == "system":
|
210 |
+
i = 1
|
211 |
+
|
212 |
+
while i < len(messages) - 1:
|
213 |
+
if messages[i]["role"] == "user" and messages[i+1]["role"] == "assistant":
|
214 |
+
new_history.append((messages[i]["content"], messages[i+1]["content"]))
|
215 |
+
i += 2
|
216 |
+
else:
|
217 |
+
i += 1
|
218 |
+
|
219 |
+
return new_history
|
220 |
+
except Exception as e:
|
221 |
+
raise gr.Error(f"Error importing chat: {str(e)}")
|
222 |
|
223 |
# Create the Chatbot component.
|
224 |
chatbot = gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='HealthAssistant')
|
|
|
230 |
# Add the checkbox directly to the layout
|
231 |
fast_mode_checkbox = gr.Checkbox(label="Fast Mode (Skips Reasoning. Provides Immediate, Less Accurate Responses.) RECOMMENDED FOR TALK THERAPY.", value=False)
|
232 |
|
233 |
+
chat_interface = gr.ChatInterface(
|
234 |
fn=chat_with_openai,
|
235 |
chatbot=chatbot,
|
236 |
fill_height=True,
|
|
|
248 |
],
|
249 |
cache_examples=False,
|
250 |
)
|
251 |
+
|
252 |
+
# Add export and import buttons
|
253 |
+
with gr.Row():
|
254 |
+
export_btn = gr.Button("Export Chat")
|
255 |
+
import_btn = gr.UploadButton("Import Chat", file_types=[".jsonl"])
|
256 |
+
|
257 |
+
# Connect buttons to functions
|
258 |
+
export_btn.click(fn=export_chat, inputs=chatbot, outputs=gr.File())
|
259 |
+
import_btn.upload(fn=import_chat, inputs=[import_btn], outputs=chatbot)
|
260 |
|
261 |
gr.Markdown(LICENSE)
|
262 |
|