Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,45 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from datetime import datetime
|
| 3 |
|
| 4 |
-
#
|
| 5 |
conversation_history = []
|
| 6 |
|
| 7 |
-
#
|
| 8 |
def log_to_file(message):
|
| 9 |
with open("chat_log.txt", "a") as file:
|
| 10 |
file.write(f"{datetime.now()} - {message}\n")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
log_to_file(f"User: {message}")
|
| 20 |
log_to_file(f"AI: {response}")
|
| 21 |
|
| 22 |
return response
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
from datetime import datetime
|
| 4 |
|
| 5 |
+
# Memory
|
| 6 |
conversation_history = []
|
| 7 |
|
| 8 |
+
# Logging
|
| 9 |
def log_to_file(message):
|
| 10 |
with open("chat_log.txt", "a") as file:
|
| 11 |
file.write(f"{datetime.now()} - {message}\n")
|
| 12 |
|
| 13 |
+
# Core function
|
| 14 |
+
def respond(message, file):
|
| 15 |
+
response = ""
|
| 16 |
|
| 17 |
+
# If there's a file, try reading its contents
|
| 18 |
+
if file is not None:
|
| 19 |
+
try:
|
| 20 |
+
content = file.read().decode("utf-8")
|
| 21 |
+
response += f"📂 Opened file {file.name}:\n\n{content[:500]}...\n\n"
|
| 22 |
+
except Exception as e:
|
| 23 |
+
response += f"⚠ Error reading file: {e}\n\n"
|
| 24 |
+
|
| 25 |
+
# Continue with regular chat
|
| 26 |
+
response += f"🤖 You said: {message}"
|
| 27 |
|
| 28 |
+
conversation_history.append((message, response))
|
| 29 |
log_to_file(f"User: {message}")
|
| 30 |
log_to_file(f"AI: {response}")
|
| 31 |
|
| 32 |
return response
|
| 33 |
|
| 34 |
+
# UI
|
| 35 |
+
iface = gr.Interface(
|
| 36 |
+
fn=respond,
|
| 37 |
+
inputs=[
|
| 38 |
+
gr.Textbox(label="Message"),
|
| 39 |
+
gr.File(label="Optional: Upload a File"),
|
| 40 |
+
],
|
| 41 |
+
outputs="text",
|
| 42 |
+
title="AI Assistant v2 - File Support"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
iface.launch()
|