luminoussg commited on
Commit
bdddd02
·
verified ·
1 Parent(s): 156fde9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -3
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  import os
 
3
  import shutil
4
  import threading
5
  from datetime import datetime
@@ -68,6 +69,7 @@ def query_model(model_name: str, messages: List[Dict[str, str]]) -> Generator[st
68
  def respond(message: str, history: List[List[str]], session_id: str) -> Generator[str, None, None]:
69
  """Handle sequential model responses with context preservation and streaming"""
70
  # Load or initialize session
 
71
  session = session_manager.load_session(session_id)
72
  if not isinstance(session, dict) or "history" not in session:
73
  session = {"history": []}
@@ -82,6 +84,10 @@ def respond(message: str, history: List[List[str]], session_id: str) -> Generato
82
 
83
  # Add current message
84
  messages.append({"role": "user", "content": message})
 
 
 
 
85
  session["history"].append({
86
  "timestamp": datetime.now().isoformat(),
87
  "type": "user",
@@ -142,8 +148,14 @@ with gr.Blocks() as demo:
142
 
143
  def user(message, file, history, session_id, save_history):
144
  if file:
145
- # Move uploaded file to uploads directory
 
 
 
 
 
146
  message += f"\nUploaded file: {file}"
 
147
  if save_history:
148
  session = session_manager.load_session(session_id)
149
  session["history"].append({
@@ -151,8 +163,9 @@ with gr.Blocks() as demo:
151
  "type": "user",
152
  "content": message
153
  })
154
- session_manager.save_session(session_id, session)
155
- return "", history + [[message, None]]
 
156
 
157
  def bot(history, session_id):
158
  if history and history[-1][1] is None:
@@ -169,5 +182,6 @@ with gr.Blocks() as demo:
169
  bot, [chatbot, session_id], [chatbot]
170
  )
171
  new_session.click(on_new_session, None, [session_id, chatbot])
 
172
  if __name__ == "__main__":
173
  demo.launch(share=True)
 
1
  import gradio as gr
2
  import os
3
+ import io
4
  import shutil
5
  import threading
6
  from datetime import datetime
 
69
  def respond(message: str, history: List[List[str]], session_id: str) -> Generator[str, None, None]:
70
  """Handle sequential model responses with context preservation and streaming"""
71
  # Load or initialize session
72
+
73
  session = session_manager.load_session(session_id)
74
  if not isinstance(session, dict) or "history" not in session:
75
  session = {"history": []}
 
84
 
85
  # Add current message
86
  messages.append({"role": "user", "content": message})
87
+
88
+ # Add file content to message
89
+
90
+
91
  session["history"].append({
92
  "timestamp": datetime.now().isoformat(),
93
  "type": "user",
 
148
 
149
  def user(message, file, history, session_id, save_history):
150
  if file:
151
+ try:
152
+ with open(file, "r", encoding="utf-8") as f:
153
+ file_content = f.read()
154
+ message += f"\nFile content: {file_content}"
155
+ except Exception as e:
156
+ message += f"\nError reading file: {str(e)}"
157
  message += f"\nUploaded file: {file}"
158
+
159
  if save_history:
160
  session = session_manager.load_session(session_id)
161
  session["history"].append({
 
163
  "type": "user",
164
  "content": message
165
  })
166
+ session_manager.save_session(session_id,session)
167
+ return "", history + [[message, None]]
168
+
169
 
170
  def bot(history, session_id):
171
  if history and history[-1][1] is None:
 
182
  bot, [chatbot, session_id], [chatbot]
183
  )
184
  new_session.click(on_new_session, None, [session_id, chatbot])
185
+
186
  if __name__ == "__main__":
187
  demo.launch(share=True)