Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -59,7 +59,7 @@ def infer_sentiment(user_text: str) -> str:
|
|
| 59 |
return "positive" # default
|
| 60 |
|
| 61 |
# -----------------------------
|
| 62 |
-
# Retrieval (
|
| 63 |
# -----------------------------
|
| 64 |
def similarity(a, b):
|
| 65 |
return SequenceMatcher(None, a, b).ratio()
|
|
@@ -82,14 +82,14 @@ def best_match_quote(category: str, sentiment: str, user_text: str) -> str:
|
|
| 82 |
best_score = score
|
| 83 |
best_quote = quote
|
| 84 |
|
| 85 |
-
if best_score < 0.3:
|
| 86 |
return "I don’t have data on that specific question."
|
| 87 |
return best_quote
|
| 88 |
|
| 89 |
# -----------------------------
|
| 90 |
# Gradio callbacks
|
| 91 |
# -----------------------------
|
| 92 |
-
conversation_log = [] #
|
| 93 |
|
| 94 |
def respond(message, history, category, sentiment_choice):
|
| 95 |
if not QUOTES:
|
|
@@ -102,15 +102,10 @@ def respond(message, history, category, sentiment_choice):
|
|
| 102 |
history.append((message, bot))
|
| 103 |
return "", history
|
| 104 |
|
| 105 |
-
if sentiment_choice == "auto"
|
| 106 |
-
sent = infer_sentiment(message)
|
| 107 |
-
else:
|
| 108 |
-
sent = sentiment_choice
|
| 109 |
-
|
| 110 |
bot = best_match_quote(category, sent, message)
|
| 111 |
history.append((message, bot))
|
| 112 |
|
| 113 |
-
# log turn for export
|
| 114 |
conversation_log.append({
|
| 115 |
"timestamp": datetime.datetime.now().isoformat(),
|
| 116 |
"category": category,
|
|
@@ -118,11 +113,10 @@ def respond(message, history, category, sentiment_choice):
|
|
| 118 |
"user_message": message,
|
| 119 |
"bot_response": bot,
|
| 120 |
})
|
| 121 |
-
|
| 122 |
return "", history
|
| 123 |
|
| 124 |
def clear_chat():
|
| 125 |
-
return None
|
| 126 |
|
| 127 |
def upload_json(filepath):
|
| 128 |
global QUOTES
|
|
@@ -139,7 +133,11 @@ def upload_json(filepath):
|
|
| 139 |
return f"Error loading file: {e}", gr.update(choices=[])
|
| 140 |
|
| 141 |
def download_current():
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
try:
|
| 144 |
if DATA_PATH and os.path.exists(DATA_PATH):
|
| 145 |
return DATA_PATH
|
|
@@ -147,10 +145,14 @@ def download_current():
|
|
| 147 |
with open(tmp, "w", encoding="utf-8") as f:
|
| 148 |
json.dump(QUOTES, f, indent=2, ensure_ascii=False)
|
| 149 |
return tmp
|
| 150 |
-
except Exception:
|
|
|
|
| 151 |
return None
|
| 152 |
|
| 153 |
def export_conversation():
|
|
|
|
|
|
|
|
|
|
| 154 |
filename = "conversation_log.csv"
|
| 155 |
try:
|
| 156 |
with open(filename, "w", newline="", encoding="utf-8") as csvfile:
|
|
@@ -160,7 +162,7 @@ def export_conversation():
|
|
| 160 |
writer.writerows(conversation_log)
|
| 161 |
return filename
|
| 162 |
except Exception as e:
|
| 163 |
-
print(f"
|
| 164 |
return None
|
| 165 |
|
| 166 |
# -----------------------------
|
|
@@ -183,25 +185,36 @@ with gr.Blocks() as demo:
|
|
| 183 |
value="auto"
|
| 184 |
)
|
| 185 |
|
|
|
|
| 186 |
chatbot = gr.Chatbot(label="Conversation", height=360, type="tuples")
|
| 187 |
msg = gr.Textbox(label="Your message", placeholder="Ask something like: 'Is food good in college?'", autofocus=True)
|
| 188 |
send = gr.Button("Send")
|
| 189 |
clear = gr.Button("Clear")
|
| 190 |
|
|
|
|
| 191 |
with gr.Row():
|
| 192 |
uploader = gr.File(label="Upload dataset (.json)", file_types=[".json"], type="filepath")
|
| 193 |
upload_status = gr.Textbox(label="Upload status", interactive=False)
|
| 194 |
-
downloader = gr.File(label="Download current dataset")
|
| 195 |
-
csv_exporter = gr.File(label="Export conversation (.csv)")
|
| 196 |
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
msg.submit(respond, [msg, chatbot, category, sentiment], [msg, chatbot])
|
| 199 |
send.click(respond, [msg, chatbot, category, sentiment], [msg, chatbot])
|
| 200 |
clear.click(clear_chat, None, chatbot, queue=False)
|
| 201 |
|
|
|
|
| 202 |
uploader.upload(upload_json, uploader, [upload_status, category])
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
| 205 |
|
| 206 |
# -----------------------------
|
| 207 |
# Startup log
|
|
|
|
| 59 |
return "positive" # default
|
| 60 |
|
| 61 |
# -----------------------------
|
| 62 |
+
# Retrieval (simple fuzzy match)
|
| 63 |
# -----------------------------
|
| 64 |
def similarity(a, b):
|
| 65 |
return SequenceMatcher(None, a, b).ratio()
|
|
|
|
| 82 |
best_score = score
|
| 83 |
best_quote = quote
|
| 84 |
|
| 85 |
+
if best_score < 0.3:
|
| 86 |
return "I don’t have data on that specific question."
|
| 87 |
return best_quote
|
| 88 |
|
| 89 |
# -----------------------------
|
| 90 |
# Gradio callbacks
|
| 91 |
# -----------------------------
|
| 92 |
+
conversation_log = [] # for CSV export
|
| 93 |
|
| 94 |
def respond(message, history, category, sentiment_choice):
|
| 95 |
if not QUOTES:
|
|
|
|
| 102 |
history.append((message, bot))
|
| 103 |
return "", history
|
| 104 |
|
| 105 |
+
sent = infer_sentiment(message) if sentiment_choice == "auto" else sentiment_choice
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
bot = best_match_quote(category, sent, message)
|
| 107 |
history.append((message, bot))
|
| 108 |
|
|
|
|
| 109 |
conversation_log.append({
|
| 110 |
"timestamp": datetime.datetime.now().isoformat(),
|
| 111 |
"category": category,
|
|
|
|
| 113 |
"user_message": message,
|
| 114 |
"bot_response": bot,
|
| 115 |
})
|
|
|
|
| 116 |
return "", history
|
| 117 |
|
| 118 |
def clear_chat():
|
| 119 |
+
return None # resets Chatbot
|
| 120 |
|
| 121 |
def upload_json(filepath):
|
| 122 |
global QUOTES
|
|
|
|
| 133 |
return f"Error loading file: {e}", gr.update(choices=[])
|
| 134 |
|
| 135 |
def download_current():
|
| 136 |
+
"""
|
| 137 |
+
Return a filepath to download:
|
| 138 |
+
- If DATA_PATH exists, serve it.
|
| 139 |
+
- Otherwise, write QUOTES to a temp JSON and serve that.
|
| 140 |
+
"""
|
| 141 |
try:
|
| 142 |
if DATA_PATH and os.path.exists(DATA_PATH):
|
| 143 |
return DATA_PATH
|
|
|
|
| 145 |
with open(tmp, "w", encoding="utf-8") as f:
|
| 146 |
json.dump(QUOTES, f, indent=2, ensure_ascii=False)
|
| 147 |
return tmp
|
| 148 |
+
except Exception as e:
|
| 149 |
+
print(f"Download error: {e}")
|
| 150 |
return None
|
| 151 |
|
| 152 |
def export_conversation():
|
| 153 |
+
"""
|
| 154 |
+
Write conversation_log to CSV and return the filepath.
|
| 155 |
+
"""
|
| 156 |
filename = "conversation_log.csv"
|
| 157 |
try:
|
| 158 |
with open(filename, "w", newline="", encoding="utf-8") as csvfile:
|
|
|
|
| 162 |
writer.writerows(conversation_log)
|
| 163 |
return filename
|
| 164 |
except Exception as e:
|
| 165 |
+
print(f"CSV export error: {e}")
|
| 166 |
return None
|
| 167 |
|
| 168 |
# -----------------------------
|
|
|
|
| 185 |
value="auto"
|
| 186 |
)
|
| 187 |
|
| 188 |
+
# (Keep the chatbot & inputs exactly as before)
|
| 189 |
chatbot = gr.Chatbot(label="Conversation", height=360, type="tuples")
|
| 190 |
msg = gr.Textbox(label="Your message", placeholder="Ask something like: 'Is food good in college?'", autofocus=True)
|
| 191 |
send = gr.Button("Send")
|
| 192 |
clear = gr.Button("Clear")
|
| 193 |
|
| 194 |
+
# Data I/O area (fix: use Button + non-interactive File outputs)
|
| 195 |
with gr.Row():
|
| 196 |
uploader = gr.File(label="Upload dataset (.json)", file_types=[".json"], type="filepath")
|
| 197 |
upload_status = gr.Textbox(label="Upload status", interactive=False)
|
|
|
|
|
|
|
| 198 |
|
| 199 |
+
with gr.Row():
|
| 200 |
+
download_btn = gr.Button("Download current dataset")
|
| 201 |
+
download_file = gr.File(label="Dataset file (click to download)", interactive=False)
|
| 202 |
+
|
| 203 |
+
with gr.Row():
|
| 204 |
+
export_btn = gr.Button("Export conversation (.csv)")
|
| 205 |
+
export_file = gr.File(label="Conversation CSV (click to download)", interactive=False)
|
| 206 |
+
|
| 207 |
+
# Wiring (unchanged chat)
|
| 208 |
msg.submit(respond, [msg, chatbot, category, sentiment], [msg, chatbot])
|
| 209 |
send.click(respond, [msg, chatbot, category, sentiment], [msg, chatbot])
|
| 210 |
clear.click(clear_chat, None, chatbot, queue=False)
|
| 211 |
|
| 212 |
+
# Upload stays the same
|
| 213 |
uploader.upload(upload_json, uploader, [upload_status, category])
|
| 214 |
+
|
| 215 |
+
# NEW: Buttons trigger functions; results populate non-interactive File outputs
|
| 216 |
+
download_btn.click(download_current, outputs=download_file)
|
| 217 |
+
export_btn.click(export_conversation, outputs=export_file)
|
| 218 |
|
| 219 |
# -----------------------------
|
| 220 |
# Startup log
|