Update app.py
Browse files
app.py
CHANGED
|
@@ -19,6 +19,8 @@ import torch
|
|
| 19 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 20 |
from PIL import Image
|
| 21 |
import torch
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Load model and processor
|
| 24 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
|
@@ -112,14 +114,25 @@ def synthesize_speech(text):
|
|
| 112 |
|
| 113 |
def export_chat(history):
|
| 114 |
from datetime import datetime
|
|
|
|
| 115 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 116 |
-
file_path = f"
|
| 117 |
with open(file_path, "w", encoding="utf-8") as f:
|
| 118 |
-
|
| 119 |
-
f.write(f"User: {user_msg}\n")
|
| 120 |
-
f.write(f"Bot: {bot_msg}\n\n")
|
| 121 |
return file_path
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
def read_uploaded_file(file_path):
|
| 124 |
if file_path:
|
| 125 |
mime_type, _ = mimetypes.guess_type(file_path)
|
|
@@ -410,6 +423,7 @@ def create_chatbot():
|
|
| 410 |
tts = gTTS(reply)
|
| 411 |
audio_filename = f"/tmp/{uuid.uuid4().hex}.mp3"
|
| 412 |
tts.save(audio_filename)
|
|
|
|
| 413 |
|
| 414 |
return messages_for_gradio, updated_history, audio_filename
|
| 415 |
|
|
@@ -713,6 +727,19 @@ def create_chatbot():
|
|
| 713 |
<div id="neon-text">PR1ME</div>
|
| 714 |
""")
|
| 715 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 716 |
|
| 717 |
return demo
|
| 718 |
|
|
|
|
| 19 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 20 |
from PIL import Image
|
| 21 |
import torch
|
| 22 |
+
import json
|
| 23 |
+
|
| 24 |
|
| 25 |
# Load model and processor
|
| 26 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
|
|
|
| 114 |
|
| 115 |
def export_chat(history):
|
| 116 |
from datetime import datetime
|
| 117 |
+
os.makedirs("history", exist_ok=True)
|
| 118 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 119 |
+
file_path = os.path.join("history", f"chat_{timestamp}.json")
|
| 120 |
with open(file_path, "w", encoding="utf-8") as f:
|
| 121 |
+
json.dump(history, f)
|
|
|
|
|
|
|
| 122 |
return file_path
|
| 123 |
|
| 124 |
+
def list_chat_sessions():
|
| 125 |
+
files = os.listdir("history")
|
| 126 |
+
return sorted([f for f in files if f.endswith(".json")], reverse=True)
|
| 127 |
+
|
| 128 |
+
def load_chat_session(file_name):
|
| 129 |
+
path = os.path.join("history", file_name)
|
| 130 |
+
try:
|
| 131 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 132 |
+
return json.load(f)
|
| 133 |
+
except Exception as e:
|
| 134 |
+
return []
|
| 135 |
+
|
| 136 |
def read_uploaded_file(file_path):
|
| 137 |
if file_path:
|
| 138 |
mime_type, _ = mimetypes.guess_type(file_path)
|
|
|
|
| 423 |
tts = gTTS(reply)
|
| 424 |
audio_filename = f"/tmp/{uuid.uuid4().hex}.mp3"
|
| 425 |
tts.save(audio_filename)
|
| 426 |
+
export_chat(history)
|
| 427 |
|
| 428 |
return messages_for_gradio, updated_history, audio_filename
|
| 429 |
|
|
|
|
| 727 |
<div id="neon-text">PR1ME</div>
|
| 728 |
""")
|
| 729 |
|
| 730 |
+
with gr.Tab("Chat History"):
|
| 731 |
+
history_dropdown = gr.Dropdown(choices=list_chat_sessions(), label="Select a session")
|
| 732 |
+
load_button = gr.Button("Load Chat")
|
| 733 |
+
history_chatbot = gr.Chatbot(type="messages", label="Loaded Chat")
|
| 734 |
+
resume_button = gr.Button("Resume in Chatbot")
|
| 735 |
+
|
| 736 |
+
def load_selected_history(file_name):
|
| 737 |
+
history = load_chat_session(file_name)
|
| 738 |
+
return format_history_for_gradio(history), history
|
| 739 |
+
|
| 740 |
+
load_button.click(fn=load_selected_history, inputs=history_dropdown, outputs=[history_chatbot, state])
|
| 741 |
+
resume_button.click(fn=lambda history: (format_history_for_gradio(history), history),
|
| 742 |
+
inputs=[state], outputs=[chatbot, state])
|
| 743 |
|
| 744 |
return demo
|
| 745 |
|