Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,19 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
|
|
3 |
import threading
|
4 |
from datetime import datetime
|
5 |
from typing import List, Dict, Any, Generator
|
6 |
from session_manager import SessionManager
|
7 |
from huggingface_hub import InferenceClient
|
8 |
-
from textbox_with_upload import TextboxWithUpload
|
9 |
-
|
10 |
-
# Check Gradio version
|
11 |
-
print(f"Gradio version: {gr.__version__}")
|
12 |
|
13 |
# Initialize session manager and get HF API key
|
14 |
session_manager = SessionManager()
|
15 |
HF_API_KEY = os.getenv("HF_API_KEY")
|
16 |
|
|
|
|
|
|
|
17 |
# Model endpoints configuration
|
18 |
MODEL_ENDPOINTS = {
|
19 |
"Qwen2.5-72B-Instruct": "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-72B-Instruct",
|
@@ -133,15 +133,17 @@ with gr.Blocks() as demo:
|
|
133 |
session_id = gr.State(session_manager.create_session)
|
134 |
new_session = gr.Button("π New Session")
|
135 |
|
136 |
-
chatbot = gr.Chatbot(height=600
|
137 |
-
msg = TextboxWithUpload(label="Message")
|
138 |
save_history = gr.Checkbox(label="Save Conversation History", value=True)
|
139 |
|
140 |
def on_new_session():
|
141 |
new_id = session_manager.create_session()
|
142 |
return new_id, []
|
143 |
|
144 |
-
def user(message, history, session_id, save_history):
|
|
|
|
|
|
|
145 |
if save_history:
|
146 |
session = session_manager.load_session(session_id)
|
147 |
session["history"].append({
|
@@ -150,11 +152,6 @@ with gr.Blocks() as demo:
|
|
150 |
"content": message
|
151 |
})
|
152 |
session_manager.save_session(session_id, session)
|
153 |
-
|
154 |
-
# Handle file upload
|
155 |
-
if message.startswith("Uploaded file:"):
|
156 |
-
message = f"I've uploaded a file: {message.split(':', 1)[1].strip()}"
|
157 |
-
|
158 |
return "", history + [[message, None]]
|
159 |
|
160 |
def bot(history, session_id):
|
@@ -164,15 +161,13 @@ with gr.Blocks() as demo:
|
|
164 |
history[-1][1] = response
|
165 |
yield history
|
166 |
|
167 |
-
|
168 |
-
|
|
|
169 |
|
170 |
-
|
171 |
-
msg.submit(user, [msg, chatbot, session_id, save_history], [msg, chatbot]).then(
|
172 |
bot, [chatbot, session_id], [chatbot]
|
173 |
)
|
174 |
-
msg.upload_button.upload(process_upload, msg.upload_button, msg) # Handle file uploads
|
175 |
new_session.click(on_new_session, None, [session_id, chatbot])
|
176 |
-
|
177 |
if __name__ == "__main__":
|
178 |
demo.launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
+
import shutil
|
4 |
import threading
|
5 |
from datetime import datetime
|
6 |
from typing import List, Dict, Any, Generator
|
7 |
from session_manager import SessionManager
|
8 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Initialize session manager and get HF API key
|
11 |
session_manager = SessionManager()
|
12 |
HF_API_KEY = os.getenv("HF_API_KEY")
|
13 |
|
14 |
+
# Create uploads directory if it doesn't exist
|
15 |
+
os.makedirs("uploads", exist_ok=True)
|
16 |
+
|
17 |
# Model endpoints configuration
|
18 |
MODEL_ENDPOINTS = {
|
19 |
"Qwen2.5-72B-Instruct": "https://api-inference.huggingface.co/models/Qwen/Qwen2.5-72B-Instruct",
|
|
|
133 |
session_id = gr.State(session_manager.create_session)
|
134 |
new_session = gr.Button("π New Session")
|
135 |
|
136 |
+
chatbot = gr.Chatbot(height=600)
|
|
|
137 |
save_history = gr.Checkbox(label="Save Conversation History", value=True)
|
138 |
|
139 |
def on_new_session():
|
140 |
new_id = session_manager.create_session()
|
141 |
return new_id, []
|
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({
|
|
|
152 |
"content": message
|
153 |
})
|
154 |
session_manager.save_session(session_id, session)
|
|
|
|
|
|
|
|
|
|
|
155 |
return "", history + [[message, None]]
|
156 |
|
157 |
def bot(history, session_id):
|
|
|
161 |
history[-1][1] = response
|
162 |
yield history
|
163 |
|
164 |
+
with gr.Row():
|
165 |
+
msg = gr.Textbox(label="Message")
|
166 |
+
file_upload = gr.UploadButton("Upload file")
|
167 |
|
168 |
+
msg.submit(user, [msg, file_upload, chatbot, session_id, save_history], [msg, chatbot]).then(
|
|
|
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)
|