Update app.py
Browse files
app.py
CHANGED
@@ -82,9 +82,9 @@ class ChatbotManager:
|
|
82 |
self.conversation_history = []
|
83 |
return "", []
|
84 |
|
85 |
-
def export_conversation(self, history: List[Tuple[str, str]]) -> str:
|
86 |
if not history:
|
87 |
-
return "No conversation to export"
|
88 |
|
89 |
export_data = {
|
90 |
"timestamp": datetime.now().isoformat(),
|
@@ -98,14 +98,13 @@ class ChatbotManager:
|
|
98 |
filename = f"conversation_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
99 |
|
100 |
try:
|
101 |
-
# For Hugging Face Spaces, use /tmp directory for writing files
|
102 |
os.makedirs("/tmp", exist_ok=True)
|
103 |
filepath = os.path.join("/tmp", filename)
|
104 |
with open(filepath, 'w', encoding='utf-8') as f:
|
105 |
json.dump(export_data, f, indent=2, ensure_ascii=False)
|
106 |
-
return f"β
Conversation exported
|
107 |
except Exception as e:
|
108 |
-
return f"β Export failed: {str(e)}"
|
109 |
|
110 |
# Initialize chatbot manager
|
111 |
chatbot = ChatbotManager()
|
@@ -253,7 +252,7 @@ def create_interface():
|
|
253 |
### 4. **Chat Features**
|
254 |
- Type messages and get intelligent responses
|
255 |
- Clear conversation history anytime
|
256 |
-
- Export chat history as JSON (
|
257 |
- Regenerate the last response
|
258 |
- Copy responses using the copy button
|
259 |
|
@@ -262,7 +261,7 @@ def create_interface():
|
|
262 |
- **Multi-model support**: GPT-3.5, GPT-4, and variants
|
263 |
- **Conversation memory**: Maintains context throughout the session
|
264 |
- **Custom data integration**: Enhance responses with your own data
|
265 |
-
- **Export functionality**: Save conversations
|
266 |
- **Real-time validation**: API key and settings verification
|
267 |
- **Visual indicators**: User and AI avatars
|
268 |
|
@@ -297,7 +296,7 @@ def create_interface():
|
|
297 |
|
298 |
def handle_chat(user_input, history):
|
299 |
if not user_input.strip():
|
300 |
-
return history, ""
|
301 |
|
302 |
response, updated_history = chatbot.generate_response(user_input, history or [])
|
303 |
return updated_history, ""
|
@@ -315,7 +314,8 @@ def create_interface():
|
|
315 |
return chatbot.clear_conversation()
|
316 |
|
317 |
def handle_export(history):
|
318 |
-
|
|
|
319 |
|
320 |
def handle_regenerate(history):
|
321 |
if not history:
|
@@ -381,7 +381,7 @@ def create_interface():
|
|
381 |
export_btn.click(
|
382 |
handle_export,
|
383 |
inputs=[chatbot_interface],
|
384 |
-
outputs=[settings_status]
|
385 |
)
|
386 |
|
387 |
regenerate_btn.click(
|
@@ -425,11 +425,7 @@ def create_interface():
|
|
425 |
return demo
|
426 |
|
427 |
if __name__ == "__main__":
|
428 |
-
# Create and launch the interface
|
429 |
demo = create_interface()
|
430 |
-
|
431 |
-
# Launch with settings suitable for Hugging Face Spaces
|
432 |
demo.launch(
|
433 |
-
|
434 |
-
server_port=7860
|
435 |
)
|
|
|
82 |
self.conversation_history = []
|
83 |
return "", []
|
84 |
|
85 |
+
def export_conversation(self, history: List[Tuple[str, str]]) -> Tuple[str, Optional[str]]:
|
86 |
if not history:
|
87 |
+
return "No conversation to export", None
|
88 |
|
89 |
export_data = {
|
90 |
"timestamp": datetime.now().isoformat(),
|
|
|
98 |
filename = f"conversation_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
99 |
|
100 |
try:
|
|
|
101 |
os.makedirs("/tmp", exist_ok=True)
|
102 |
filepath = os.path.join("/tmp", filename)
|
103 |
with open(filepath, 'w', encoding='utf-8') as f:
|
104 |
json.dump(export_data, f, indent=2, ensure_ascii=False)
|
105 |
+
return f"β
Conversation exported as {filename}", filepath
|
106 |
except Exception as e:
|
107 |
+
return f"β Export failed: {str(e)}", None
|
108 |
|
109 |
# Initialize chatbot manager
|
110 |
chatbot = ChatbotManager()
|
|
|
252 |
### 4. **Chat Features**
|
253 |
- Type messages and get intelligent responses
|
254 |
- Clear conversation history anytime
|
255 |
+
- Export chat history as JSON (downloads automatically)
|
256 |
- Regenerate the last response
|
257 |
- Copy responses using the copy button
|
258 |
|
|
|
261 |
- **Multi-model support**: GPT-3.5, GPT-4, and variants
|
262 |
- **Conversation memory**: Maintains context throughout the session
|
263 |
- **Custom data integration**: Enhance responses with your own data
|
264 |
+
- **Export functionality**: Save conversations as JSON downloads
|
265 |
- **Real-time validation**: API key and settings verification
|
266 |
- **Visual indicators**: User and AI avatars
|
267 |
|
|
|
296 |
|
297 |
def handle_chat(user_input, history):
|
298 |
if not user_input.strip():
|
299 |
+
return history or [], ""
|
300 |
|
301 |
response, updated_history = chatbot.generate_response(user_input, history or [])
|
302 |
return updated_history, ""
|
|
|
314 |
return chatbot.clear_conversation()
|
315 |
|
316 |
def handle_export(history):
|
317 |
+
status, filepath = chatbot.export_conversation(history or [])
|
318 |
+
return status, filepath
|
319 |
|
320 |
def handle_regenerate(history):
|
321 |
if not history:
|
|
|
381 |
export_btn.click(
|
382 |
handle_export,
|
383 |
inputs=[chatbot_interface],
|
384 |
+
outputs=[settings_status, gr.File(visible=False)]
|
385 |
)
|
386 |
|
387 |
regenerate_btn.click(
|
|
|
425 |
return demo
|
426 |
|
427 |
if __name__ == "__main__":
|
|
|
428 |
demo = create_interface()
|
|
|
|
|
429 |
demo.launch(
|
430 |
+
share=True
|
|
|
431 |
)
|