Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,17 +9,20 @@ from utils import process_image, process_video, generate_references
|
|
| 9 |
|
| 10 |
loader = ModelLoader()
|
| 11 |
|
| 12 |
-
# ===================== CHAT FUNCTION
|
| 13 |
def chat(message: str, history, file=None):
|
| 14 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 15 |
if history is None:
|
| 16 |
history = []
|
| 17 |
-
|
| 18 |
if not message.strip() and not file:
|
| 19 |
-
history.append(
|
| 20 |
return history
|
| 21 |
|
| 22 |
-
# --- Process File
|
| 23 |
context = ""
|
| 24 |
if file:
|
| 25 |
try:
|
|
@@ -34,9 +37,9 @@ def chat(message: str, history, file=None):
|
|
| 34 |
summary = process_video(file_path)
|
| 35 |
context = f"**Video Analysis**:\n{summary}\n\n"
|
| 36 |
else:
|
| 37 |
-
context = "**File uploaded
|
| 38 |
except Exception as e:
|
| 39 |
-
context = f"**File
|
| 40 |
|
| 41 |
# --- Generate Response ---
|
| 42 |
full_prompt = f"{context}User: {message}\n\nAssistant:"
|
|
@@ -62,12 +65,13 @@ def chat(message: str, history, file=None):
|
|
| 62 |
final_response = f"{response_text.strip()}\n\n**Sources / References:**\n{generate_references(message[:100])}"
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
-
final_response = f"⚠️ Error
|
| 66 |
|
| 67 |
-
# Append
|
| 68 |
-
history.append(
|
|
|
|
| 69 |
|
| 70 |
-
#
|
| 71 |
gc.collect()
|
| 72 |
if torch.cuda.is_available():
|
| 73 |
torch.cuda.empty_cache()
|
|
@@ -75,7 +79,7 @@ def chat(message: str, history, file=None):
|
|
| 75 |
return history
|
| 76 |
|
| 77 |
|
| 78 |
-
# =====================
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
gr.Markdown("# 🛡️ Private Personal AI Chatbot\n**Fully Local • Private • Open Source**")
|
| 81 |
|
|
@@ -83,11 +87,12 @@ with gr.Blocks() as demo:
|
|
| 83 |
height=650,
|
| 84 |
avatar_images=(None, "🧠"),
|
| 85 |
label=None
|
|
|
|
| 86 |
)
|
| 87 |
|
| 88 |
with gr.Row():
|
| 89 |
msg = gr.Textbox(
|
| 90 |
-
placeholder="Ask anything... (
|
| 91 |
scale=4,
|
| 92 |
container=False,
|
| 93 |
autofocus=True
|
|
@@ -109,9 +114,9 @@ with gr.Blocks() as demo:
|
|
| 109 |
inputs=[msg, file_upload]
|
| 110 |
)
|
| 111 |
|
| 112 |
-
gr.Markdown("**Privacy**:
|
| 113 |
|
| 114 |
-
# =====================
|
| 115 |
submit_btn.click(
|
| 116 |
chat,
|
| 117 |
inputs=[msg, chatbot, file_upload],
|
|
|
|
| 9 |
|
| 10 |
loader = ModelLoader()
|
| 11 |
|
| 12 |
+
# ===================== CHAT FUNCTION =====================
|
| 13 |
def chat(message: str, history, file=None):
|
| 14 |
+
"""
|
| 15 |
+
Works with current HF Spaces Gradio (mixed 5/6 behavior)
|
| 16 |
+
Returns messages format (dict with role/content)
|
| 17 |
+
"""
|
| 18 |
if history is None:
|
| 19 |
history = []
|
| 20 |
+
|
| 21 |
if not message.strip() and not file:
|
| 22 |
+
history.append({"role": "assistant", "content": "Please type a message or upload a file."})
|
| 23 |
return history
|
| 24 |
|
| 25 |
+
# --- Process File ---
|
| 26 |
context = ""
|
| 27 |
if file:
|
| 28 |
try:
|
|
|
|
| 37 |
summary = process_video(file_path)
|
| 38 |
context = f"**Video Analysis**:\n{summary}\n\n"
|
| 39 |
else:
|
| 40 |
+
context = "**File uploaded.**\n\n"
|
| 41 |
except Exception as e:
|
| 42 |
+
context = f"**File error**: {str(e)[:100]}\n\n"
|
| 43 |
|
| 44 |
# --- Generate Response ---
|
| 45 |
full_prompt = f"{context}User: {message}\n\nAssistant:"
|
|
|
|
| 65 |
final_response = f"{response_text.strip()}\n\n**Sources / References:**\n{generate_references(message[:100])}"
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
+
final_response = f"⚠️ Error: {str(e)[:180]}"
|
| 69 |
|
| 70 |
+
# Append in MESSAGES FORMAT (what Gradio currently expects)
|
| 71 |
+
history.append({"role": "user", "content": message})
|
| 72 |
+
history.append({"role": "assistant", "content": final_response})
|
| 73 |
|
| 74 |
+
# Cleanup
|
| 75 |
gc.collect()
|
| 76 |
if torch.cuda.is_available():
|
| 77 |
torch.cuda.empty_cache()
|
|
|
|
| 79 |
return history
|
| 80 |
|
| 81 |
|
| 82 |
+
# ===================== UI =====================
|
| 83 |
with gr.Blocks() as demo:
|
| 84 |
gr.Markdown("# 🛡️ Private Personal AI Chatbot\n**Fully Local • Private • Open Source**")
|
| 85 |
|
|
|
|
| 87 |
height=650,
|
| 88 |
avatar_images=(None, "🧠"),
|
| 89 |
label=None
|
| 90 |
+
# NO 'type' argument → avoids init error
|
| 91 |
)
|
| 92 |
|
| 93 |
with gr.Row():
|
| 94 |
msg = gr.Textbox(
|
| 95 |
+
placeholder="Ask anything... (supports image & video)",
|
| 96 |
scale=4,
|
| 97 |
container=False,
|
| 98 |
autofocus=True
|
|
|
|
| 114 |
inputs=[msg, file_upload]
|
| 115 |
)
|
| 116 |
|
| 117 |
+
gr.Markdown("**Privacy**: No data is stored or logged.")
|
| 118 |
|
| 119 |
+
# ===================== EVENTS =====================
|
| 120 |
submit_btn.click(
|
| 121 |
chat,
|
| 122 |
inputs=[msg, chatbot, file_upload],
|