Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -51,7 +51,6 @@ else:
|
|
| 51 |
|
| 52 |
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200, add_start_index=True)
|
| 53 |
|
| 54 |
-
|
| 55 |
# ---------------- GROQ LLM ----------------
|
| 56 |
|
| 57 |
def query_groq_llm(prompt: str) -> str:
|
|
@@ -112,19 +111,22 @@ def generate_image_from_prompt(prompt: str, api_token: str, output_path="funny_o
|
|
| 112 |
response = requests.post(url, headers=headers, json=payload)
|
| 113 |
response.raise_for_status()
|
| 114 |
|
| 115 |
-
# Save image
|
| 116 |
with open(output_path, "wb") as f:
|
| 117 |
f.write(response.content)
|
| 118 |
|
| 119 |
return output_path
|
| 120 |
-
|
| 121 |
# ---------------- DUCKDUCKGO TOOL ----------------
|
| 122 |
|
| 123 |
@tool
|
| 124 |
def search_tool(query: str) -> str:
|
| 125 |
"""Search the web using DuckDuckGo."""
|
| 126 |
-
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
# ---------------- UTILS ----------------
|
| 130 |
|
|
@@ -137,9 +139,6 @@ def is_vague(text: str) -> bool:
|
|
| 137 |
]
|
| 138 |
return any(phrase in text.lower() for phrase in vague_phrases)
|
| 139 |
|
| 140 |
-
def is_realtime_query(text: str) -> bool:
|
| 141 |
-
keywords = ["current", "today", "latest", "now", "who is", "trending", "new", "recent"]
|
| 142 |
-
return any(k in text.lower() for k in keywords)
|
| 143 |
|
| 144 |
# ---------------- FALLBACK WEB SEARCH ----------------
|
| 145 |
|
|
@@ -212,7 +211,6 @@ with gr.Blocks(title="MythBuster AI") as iface:
|
|
| 212 |
gr.Markdown("""
|
| 213 |
# π΅οΈ MythBuster AI
|
| 214 |
**Ask me about any myth, rumor, or common belief β I'll investigate it and give you a verdict!**
|
| 215 |
-
<br>
|
| 216 |
π‘ I classify myths as:
|
| 217 |
- β
**CONFIRMED**
|
| 218 |
- β **PLAUSIBLE**
|
|
@@ -222,7 +220,6 @@ with gr.Blocks(title="MythBuster AI") as iface:
|
|
| 222 |
|
| 223 |
with gr.Row():
|
| 224 |
chatbot = gr.Chatbot(label="π§ Myth Verdicts", height=400, type="messages")
|
| 225 |
-
# logbox = gr.Textbox(label="π Logs", lines=12, interactive=False)
|
| 226 |
funny_output = gr.Image(label="π Funny Image")
|
| 227 |
|
| 228 |
with gr.Row():
|
|
@@ -231,28 +228,35 @@ with gr.Blocks(title="MythBuster AI") as iface:
|
|
| 231 |
placeholder="e.g., 'Drinking cold water causes a sore throat'",
|
| 232 |
show_label=False
|
| 233 |
)
|
|
|
|
| 234 |
submit_btn = gr.Button("π Bust This Myth")
|
| 235 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
-
|
| 238 |
-
logger.info(f"User claim: {message}")
|
| 239 |
-
|
| 240 |
-
if history is None:
|
| 241 |
-
history = []
|
| 242 |
-
|
| 243 |
response = ask(message)
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
funny_prompt = generate_funny_image_prompt(message)
|
| 250 |
-
image_path = generate_image_from_prompt(funny_prompt, HF_API_TOKEN)
|
| 251 |
-
|
| 252 |
-
|
|
|
|
| 253 |
|
|
|
|
| 254 |
|
| 255 |
-
gen_image = gr.Checkbox(label="π¨ Generate Funny Image", value=True)
|
| 256 |
submit_btn.click(user_message_handler, [msg, chatbot, gen_image], [msg, chatbot, funny_output])
|
| 257 |
msg.submit(user_message_handler, [msg, chatbot, gen_image], [msg, chatbot, funny_output])
|
| 258 |
|
|
@@ -270,9 +274,8 @@ with gr.Blocks(title="MythBuster AI") as iface:
|
|
| 270 |
["The Great Wall of China is visible from space"]
|
| 271 |
],
|
| 272 |
inputs=msg,
|
| 273 |
-
|
| 274 |
-
label="Examples",
|
| 275 |
-
fn=user_message_handler
|
| 276 |
)
|
|
|
|
| 277 |
if __name__ == "__main__":
|
| 278 |
iface.launch(share=True)
|
|
|
|
| 51 |
|
| 52 |
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200, add_start_index=True)
|
| 53 |
|
|
|
|
| 54 |
# ---------------- GROQ LLM ----------------
|
| 55 |
|
| 56 |
def query_groq_llm(prompt: str) -> str:
|
|
|
|
| 111 |
response = requests.post(url, headers=headers, json=payload)
|
| 112 |
response.raise_for_status()
|
| 113 |
|
|
|
|
| 114 |
with open(output_path, "wb") as f:
|
| 115 |
f.write(response.content)
|
| 116 |
|
| 117 |
return output_path
|
|
|
|
| 118 |
# ---------------- DUCKDUCKGO TOOL ----------------
|
| 119 |
|
| 120 |
@tool
|
| 121 |
def search_tool(query: str) -> str:
|
| 122 |
"""Search the web using DuckDuckGo."""
|
| 123 |
+
try:
|
| 124 |
+
search = DuckDuckGoSearchResults()
|
| 125 |
+
return search.run(query)
|
| 126 |
+
except Exception as e:
|
| 127 |
+
logger.error(f"DuckDuckGo search failed: {e}")
|
| 128 |
+
return "No results due to error or rate limiting."
|
| 129 |
+
|
| 130 |
|
| 131 |
# ---------------- UTILS ----------------
|
| 132 |
|
|
|
|
| 139 |
]
|
| 140 |
return any(phrase in text.lower() for phrase in vague_phrases)
|
| 141 |
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
# ---------------- FALLBACK WEB SEARCH ----------------
|
| 144 |
|
|
|
|
| 211 |
gr.Markdown("""
|
| 212 |
# π΅οΈ MythBuster AI
|
| 213 |
**Ask me about any myth, rumor, or common belief β I'll investigate it and give you a verdict!**
|
|
|
|
| 214 |
π‘ I classify myths as:
|
| 215 |
- β
**CONFIRMED**
|
| 216 |
- β **PLAUSIBLE**
|
|
|
|
| 220 |
|
| 221 |
with gr.Row():
|
| 222 |
chatbot = gr.Chatbot(label="π§ Myth Verdicts", height=400, type="messages")
|
|
|
|
| 223 |
funny_output = gr.Image(label="π Funny Image")
|
| 224 |
|
| 225 |
with gr.Row():
|
|
|
|
| 228 |
placeholder="e.g., 'Drinking cold water causes a sore throat'",
|
| 229 |
show_label=False
|
| 230 |
)
|
| 231 |
+
gen_image = gr.Checkbox(label="π¨ Generate Funny Image", value=True)
|
| 232 |
submit_btn = gr.Button("π Bust This Myth")
|
| 233 |
|
| 234 |
+
def user_message_handler(message, history, generate_img):
|
| 235 |
+
logger.info(f"User claim: {message}")
|
| 236 |
+
if history is None:
|
| 237 |
+
history = []
|
| 238 |
|
| 239 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
response = ask(message)
|
| 241 |
+
except Exception as e:
|
| 242 |
+
response = f"β Error: {e}"
|
| 243 |
+
logger.error(str(e))
|
| 244 |
+
return "", history, None
|
| 245 |
+
|
| 246 |
+
history.append({"role": "user", "content": message})
|
| 247 |
+
history.append({"role": "assistant", "content": response})
|
| 248 |
+
|
| 249 |
+
image_path = None
|
| 250 |
+
if generate_img:
|
| 251 |
+
try:
|
| 252 |
funny_prompt = generate_funny_image_prompt(message)
|
| 253 |
+
image_path = generate_image_from_prompt(funny_prompt, HF_API_TOKEN)
|
| 254 |
+
except Exception as e:
|
| 255 |
+
logger.error(f"Image generation error: {e}")
|
| 256 |
+
image_path = None
|
| 257 |
|
| 258 |
+
return "", history, image_path
|
| 259 |
|
|
|
|
| 260 |
submit_btn.click(user_message_handler, [msg, chatbot, gen_image], [msg, chatbot, funny_output])
|
| 261 |
msg.submit(user_message_handler, [msg, chatbot, gen_image], [msg, chatbot, funny_output])
|
| 262 |
|
|
|
|
| 274 |
["The Great Wall of China is visible from space"]
|
| 275 |
],
|
| 276 |
inputs=msg,
|
| 277 |
+
label="Examples"
|
|
|
|
|
|
|
| 278 |
)
|
| 279 |
+
|
| 280 |
if __name__ == "__main__":
|
| 281 |
iface.launch(share=True)
|