Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import random
|
|
| 3 |
import os
|
| 4 |
import datetime
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
|
| 7 |
# -----------------------------
|
| 8 |
# Config / data loading
|
|
@@ -27,42 +28,32 @@ def tokenize(text):
|
|
| 27 |
return set(text.lower().split())
|
| 28 |
|
| 29 |
def best_match_quote(category, user_text):
|
| 30 |
-
"""Find the quote with the most token overlap."""
|
| 31 |
if category not in QUOTES:
|
| 32 |
return None
|
| 33 |
pool = QUOTES[category]
|
| 34 |
if not pool:
|
| 35 |
return None
|
| 36 |
-
|
| 37 |
q_tokens = tokenize(user_text)
|
| 38 |
best_score = -1
|
| 39 |
best_quote = None
|
| 40 |
-
|
| 41 |
for entry in pool:
|
| 42 |
quote_tokens = tokenize(entry["quote"])
|
| 43 |
score = len(q_tokens & quote_tokens)
|
| 44 |
if score > best_score:
|
| 45 |
best_score = score
|
| 46 |
best_quote = entry["quote"]
|
| 47 |
-
|
| 48 |
-
# fallback
|
| 49 |
if not best_quote:
|
| 50 |
best_quote = random.choice(pool)["quote"]
|
| 51 |
-
|
| 52 |
return best_quote
|
| 53 |
|
| 54 |
def generate_response(category, user_text):
|
| 55 |
-
"""Produce 3-fold response based on category quotes."""
|
| 56 |
if category not in QUOTES:
|
| 57 |
return ["No quotes found for this category.", "", ""]
|
| 58 |
-
|
| 59 |
pool = QUOTES[category]
|
| 60 |
if not pool:
|
| 61 |
return ["No quotes available.", "", ""]
|
| 62 |
-
|
| 63 |
-
# For simplicity, pick up to 2-3 quotes for summary
|
| 64 |
sampled = random.sample(pool, min(2, len(pool)))
|
| 65 |
-
summary = " ".join([s["quote"].split(".")[0] + "." for s in sampled])
|
| 66 |
details = " ".join([s["quote"] for s in sampled])
|
| 67 |
suggested_url = "https://www.example.com/search?q=" + category.replace(" ", "+")
|
| 68 |
return [summary, details, suggested_url]
|
|
@@ -73,23 +64,20 @@ def generate_response(category, user_text):
|
|
| 73 |
def respond(message, history, category):
|
| 74 |
if not message.strip():
|
| 75 |
return "", history
|
| 76 |
-
|
| 77 |
summary, details, suggested_url = generate_response(category, message)
|
| 78 |
-
|
| 79 |
-
# Append messages in Gradio "messages" format
|
| 80 |
history.append({"role": "user", "content": message})
|
| 81 |
history.append({"role": "assistant", "content": f"Summary:\n{summary}"})
|
| 82 |
history.append({"role": "assistant", "content": f"What real people say:\n{details}"})
|
| 83 |
history.append({"role": "assistant", "content": f"Suggested URL:\n{suggested_url}"})
|
| 84 |
-
|
| 85 |
return "", history
|
| 86 |
|
| 87 |
def clear_chat():
|
| 88 |
return None
|
| 89 |
|
|
|
|
|
|
|
|
|
|
| 90 |
def download_current_csv(history):
|
| 91 |
-
"""Export current conversation to CSV."""
|
| 92 |
-
import csv
|
| 93 |
tmp = "conversation_export.csv"
|
| 94 |
with open(tmp, "w", newline="", encoding="utf-8") as f:
|
| 95 |
writer = csv.writer(f)
|
|
@@ -98,14 +86,19 @@ def download_current_csv(history):
|
|
| 98 |
writer.writerow([msg["role"], msg["content"]])
|
| 99 |
return tmp
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
# -----------------------------
|
| 102 |
-
# UI
|
| 103 |
# -----------------------------
|
| 104 |
with gr.Blocks() as demo:
|
| 105 |
gr.Markdown("## 🎓 College Life Chatbot — 3-Fold Responses")
|
| 106 |
|
| 107 |
initial_categories = sorted(list(QUOTES.keys()))
|
| 108 |
-
|
| 109 |
with gr.Row():
|
| 110 |
category = gr.Dropdown(
|
| 111 |
label="Category",
|
|
@@ -118,14 +111,21 @@ with gr.Blocks() as demo:
|
|
| 118 |
send = gr.Button("Send")
|
| 119 |
clear = gr.Button("Clear")
|
| 120 |
|
|
|
|
|
|
|
|
|
|
| 121 |
with gr.Row():
|
| 122 |
-
|
|
|
|
| 123 |
|
| 124 |
-
# Wire events
|
| 125 |
msg.submit(respond, [msg, chatbot, category], [msg, chatbot])
|
| 126 |
send.click(respond, [msg, chatbot, category], [msg, chatbot])
|
| 127 |
clear.click(clear_chat, None, chatbot, queue=False)
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
# -----------------------------
|
| 131 |
# Startup log
|
|
|
|
| 3 |
import os
|
| 4 |
import datetime
|
| 5 |
import gradio as gr
|
| 6 |
+
import csv
|
| 7 |
|
| 8 |
# -----------------------------
|
| 9 |
# Config / data loading
|
|
|
|
| 28 |
return set(text.lower().split())
|
| 29 |
|
| 30 |
def best_match_quote(category, user_text):
|
|
|
|
| 31 |
if category not in QUOTES:
|
| 32 |
return None
|
| 33 |
pool = QUOTES[category]
|
| 34 |
if not pool:
|
| 35 |
return None
|
|
|
|
| 36 |
q_tokens = tokenize(user_text)
|
| 37 |
best_score = -1
|
| 38 |
best_quote = None
|
|
|
|
| 39 |
for entry in pool:
|
| 40 |
quote_tokens = tokenize(entry["quote"])
|
| 41 |
score = len(q_tokens & quote_tokens)
|
| 42 |
if score > best_score:
|
| 43 |
best_score = score
|
| 44 |
best_quote = entry["quote"]
|
|
|
|
|
|
|
| 45 |
if not best_quote:
|
| 46 |
best_quote = random.choice(pool)["quote"]
|
|
|
|
| 47 |
return best_quote
|
| 48 |
|
| 49 |
def generate_response(category, user_text):
|
|
|
|
| 50 |
if category not in QUOTES:
|
| 51 |
return ["No quotes found for this category.", "", ""]
|
|
|
|
| 52 |
pool = QUOTES[category]
|
| 53 |
if not pool:
|
| 54 |
return ["No quotes available.", "", ""]
|
|
|
|
|
|
|
| 55 |
sampled = random.sample(pool, min(2, len(pool)))
|
| 56 |
+
summary = " ".join([s["quote"].split(".")[0] + "." for s in sampled])
|
| 57 |
details = " ".join([s["quote"] for s in sampled])
|
| 58 |
suggested_url = "https://www.example.com/search?q=" + category.replace(" ", "+")
|
| 59 |
return [summary, details, suggested_url]
|
|
|
|
| 64 |
def respond(message, history, category):
|
| 65 |
if not message.strip():
|
| 66 |
return "", history
|
|
|
|
| 67 |
summary, details, suggested_url = generate_response(category, message)
|
|
|
|
|
|
|
| 68 |
history.append({"role": "user", "content": message})
|
| 69 |
history.append({"role": "assistant", "content": f"Summary:\n{summary}"})
|
| 70 |
history.append({"role": "assistant", "content": f"What real people say:\n{details}"})
|
| 71 |
history.append({"role": "assistant", "content": f"Suggested URL:\n{suggested_url}"})
|
|
|
|
| 72 |
return "", history
|
| 73 |
|
| 74 |
def clear_chat():
|
| 75 |
return None
|
| 76 |
|
| 77 |
+
# -----------------------------
|
| 78 |
+
# Below Clear button: Downloads / staging
|
| 79 |
+
# -----------------------------
|
| 80 |
def download_current_csv(history):
|
|
|
|
|
|
|
| 81 |
tmp = "conversation_export.csv"
|
| 82 |
with open(tmp, "w", newline="", encoding="utf-8") as f:
|
| 83 |
writer = csv.writer(f)
|
|
|
|
| 86 |
writer.writerow([msg["role"], msg["content"]])
|
| 87 |
return tmp
|
| 88 |
|
| 89 |
+
def download_current_json():
|
| 90 |
+
tmp = "quotes_export.json"
|
| 91 |
+
with open(tmp, "w", encoding="utf-8") as f:
|
| 92 |
+
json.dump(QUOTES, f, indent=2, ensure_ascii=False)
|
| 93 |
+
return tmp
|
| 94 |
+
|
| 95 |
# -----------------------------
|
| 96 |
+
# UI (lock everything above Clear button)
|
| 97 |
# -----------------------------
|
| 98 |
with gr.Blocks() as demo:
|
| 99 |
gr.Markdown("## 🎓 College Life Chatbot — 3-Fold Responses")
|
| 100 |
|
| 101 |
initial_categories = sorted(list(QUOTES.keys()))
|
|
|
|
| 102 |
with gr.Row():
|
| 103 |
category = gr.Dropdown(
|
| 104 |
label="Category",
|
|
|
|
| 111 |
send = gr.Button("Send")
|
| 112 |
clear = gr.Button("Clear")
|
| 113 |
|
| 114 |
+
# -----------------------------
|
| 115 |
+
# Downloads / staged responses
|
| 116 |
+
# -----------------------------
|
| 117 |
with gr.Row():
|
| 118 |
+
download_csv_btn = gr.File(label="Export conversation to CSV")
|
| 119 |
+
download_json_btn = gr.File(label="Download current dataset")
|
| 120 |
|
| 121 |
+
# Wire events (lock above Clear button)
|
| 122 |
msg.submit(respond, [msg, chatbot, category], [msg, chatbot])
|
| 123 |
send.click(respond, [msg, chatbot, category], [msg, chatbot])
|
| 124 |
clear.click(clear_chat, None, chatbot, queue=False)
|
| 125 |
+
|
| 126 |
+
# Downloads wiring
|
| 127 |
+
download_csv_btn.download(lambda h: download_current_csv(h), chatbot, download_csv_btn)
|
| 128 |
+
download_json_btn.download(download_current_json, None, download_json_btn)
|
| 129 |
|
| 130 |
# -----------------------------
|
| 131 |
# Startup log
|