Spaces:
Running
Running
Commit ·
fdae7a6
1
Parent(s): 1649f04
Fix Gradio 5.x ChatInterface compatibility - add type='messages'
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ import spaces
|
|
| 14 |
import torch
|
| 15 |
import faiss
|
| 16 |
from sentence_transformers import SentenceTransformer
|
| 17 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 18 |
|
| 19 |
# =============================================================================
|
| 20 |
# Configuration
|
|
@@ -182,7 +182,7 @@ def format_citations(retrieved_chunks: list[dict]) -> str:
|
|
| 182 |
# Generation with ZeroGPU
|
| 183 |
# =============================================================================
|
| 184 |
|
| 185 |
-
@spaces.GPU(duration=
|
| 186 |
def generate_response(query: str, context: str) -> str:
|
| 187 |
"""Generate response using the LLM with ZeroGPU."""
|
| 188 |
|
|
@@ -236,25 +236,25 @@ Provide a helpful answer based ONLY on the context above. If the context doesn't
|
|
| 236 |
# Chat Function
|
| 237 |
# =============================================================================
|
| 238 |
|
| 239 |
-
def chat(message: str, history: list, request: gr.Request) ->
|
| 240 |
"""Main chat function."""
|
| 241 |
|
| 242 |
# Rate limit check
|
| 243 |
allowed, error_msg = check_rate_limit(request)
|
| 244 |
if not allowed:
|
| 245 |
-
return error_msg
|
| 246 |
|
| 247 |
if not message.strip():
|
| 248 |
-
return "Please enter a question."
|
| 249 |
|
| 250 |
if faiss_index is None:
|
| 251 |
-
return "The paper index is not loaded. Please check the Space configuration."
|
| 252 |
|
| 253 |
# Retrieve relevant chunks
|
| 254 |
retrieved = retrieve(message)
|
| 255 |
|
| 256 |
if not retrieved:
|
| 257 |
-
return "I couldn't find relevant information in the indexed papers."
|
| 258 |
|
| 259 |
# Format context
|
| 260 |
context = format_context(retrieved)
|
|
@@ -263,14 +263,14 @@ def chat(message: str, history: list, request: gr.Request) -> tuple[str, list]:
|
|
| 263 |
try:
|
| 264 |
response = generate_response(message, context)
|
| 265 |
except Exception as e:
|
| 266 |
-
return f"Error generating response: {str(e)}"
|
| 267 |
|
| 268 |
# Add citations
|
| 269 |
citations = format_citations(retrieved)
|
| 270 |
if citations:
|
| 271 |
response = f"{response}\n\n**Sources:**\n{citations}"
|
| 272 |
|
| 273 |
-
return response
|
| 274 |
|
| 275 |
|
| 276 |
# =============================================================================
|
|
@@ -278,7 +278,7 @@ def chat(message: str, history: list, request: gr.Request) -> tuple[str, list]:
|
|
| 278 |
# =============================================================================
|
| 279 |
|
| 280 |
DESCRIPTION = """
|
| 281 |
-
# Ask My Research
|
| 282 |
|
| 283 |
Chat with Anthony Maio's AI safety research papers. Ask questions about:
|
| 284 |
|
|
@@ -300,45 +300,17 @@ EXAMPLES = [
|
|
| 300 |
"How do weak verifiers fail to detect deceptive reasoning?",
|
| 301 |
]
|
| 302 |
|
| 303 |
-
|
|
|
|
|
|
|
| 304 |
title="Ask My Research",
|
|
|
|
|
|
|
| 305 |
theme=gr.themes.Soft(
|
| 306 |
primary_hue="orange",
|
| 307 |
-
secondary_hue="
|
| 308 |
-
)
|
| 309 |
-
)
|
| 310 |
-
gr.Markdown(DESCRIPTION)
|
| 311 |
-
|
| 312 |
-
chatbot = gr.Chatbot(
|
| 313 |
-
label="Conversation",
|
| 314 |
-
height=400,
|
| 315 |
-
show_copy_button=True,
|
| 316 |
-
)
|
| 317 |
-
|
| 318 |
-
with gr.Row():
|
| 319 |
-
msg = gr.Textbox(
|
| 320 |
-
label="Your question",
|
| 321 |
-
placeholder="Ask about the research papers...",
|
| 322 |
-
scale=4,
|
| 323 |
-
show_label=False,
|
| 324 |
-
)
|
| 325 |
-
submit = gr.Button("Ask", variant="primary", scale=1)
|
| 326 |
-
|
| 327 |
-
gr.Examples(
|
| 328 |
-
examples=EXAMPLES,
|
| 329 |
-
inputs=msg,
|
| 330 |
-
label="Example questions"
|
| 331 |
-
)
|
| 332 |
-
|
| 333 |
-
gr.Markdown("""
|
| 334 |
-
---
|
| 335 |
-
*Rate limited to 20 questions/hour. Built by [Anthony Maio](https://making-minds.ai) | [Research Papers](https://making-minds.ai/research)*
|
| 336 |
-
""")
|
| 337 |
-
|
| 338 |
-
# Event handlers
|
| 339 |
-
msg.submit(chat, [msg, chatbot], [msg, chatbot])
|
| 340 |
-
submit.click(chat, [msg, chatbot], [msg, chatbot])
|
| 341 |
-
|
| 342 |
|
| 343 |
if __name__ == "__main__":
|
| 344 |
demo.launch()
|
|
|
|
| 14 |
import torch
|
| 15 |
import faiss
|
| 16 |
from sentence_transformers import SentenceTransformer
|
| 17 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 18 |
|
| 19 |
# =============================================================================
|
| 20 |
# Configuration
|
|
|
|
| 182 |
# Generation with ZeroGPU
|
| 183 |
# =============================================================================
|
| 184 |
|
| 185 |
+
@spaces.GPU(duration=120)
|
| 186 |
def generate_response(query: str, context: str) -> str:
|
| 187 |
"""Generate response using the LLM with ZeroGPU."""
|
| 188 |
|
|
|
|
| 236 |
# Chat Function
|
| 237 |
# =============================================================================
|
| 238 |
|
| 239 |
+
def chat(message: str, history: list, request: gr.Request) -> str:
|
| 240 |
"""Main chat function."""
|
| 241 |
|
| 242 |
# Rate limit check
|
| 243 |
allowed, error_msg = check_rate_limit(request)
|
| 244 |
if not allowed:
|
| 245 |
+
return error_msg
|
| 246 |
|
| 247 |
if not message.strip():
|
| 248 |
+
return "Please enter a question."
|
| 249 |
|
| 250 |
if faiss_index is None:
|
| 251 |
+
return "The paper index is not loaded. Please check the Space configuration."
|
| 252 |
|
| 253 |
# Retrieve relevant chunks
|
| 254 |
retrieved = retrieve(message)
|
| 255 |
|
| 256 |
if not retrieved:
|
| 257 |
+
return "I couldn't find relevant information in the indexed papers."
|
| 258 |
|
| 259 |
# Format context
|
| 260 |
context = format_context(retrieved)
|
|
|
|
| 263 |
try:
|
| 264 |
response = generate_response(message, context)
|
| 265 |
except Exception as e:
|
| 266 |
+
return f"Error generating response: {str(e)}"
|
| 267 |
|
| 268 |
# Add citations
|
| 269 |
citations = format_citations(retrieved)
|
| 270 |
if citations:
|
| 271 |
response = f"{response}\n\n**Sources:**\n{citations}"
|
| 272 |
|
| 273 |
+
return response
|
| 274 |
|
| 275 |
|
| 276 |
# =============================================================================
|
|
|
|
| 278 |
# =============================================================================
|
| 279 |
|
| 280 |
DESCRIPTION = """
|
| 281 |
+
# 🔬 Ask My Research
|
| 282 |
|
| 283 |
Chat with Anthony Maio's AI safety research papers. Ask questions about:
|
| 284 |
|
|
|
|
| 300 |
"How do weak verifiers fail to detect deceptive reasoning?",
|
| 301 |
]
|
| 302 |
|
| 303 |
+
demo = gr.ChatInterface(
|
| 304 |
+
fn=chat,
|
| 305 |
+
type="messages",
|
| 306 |
title="Ask My Research",
|
| 307 |
+
description=DESCRIPTION,
|
| 308 |
+
examples=EXAMPLES,
|
| 309 |
theme=gr.themes.Soft(
|
| 310 |
primary_hue="orange",
|
| 311 |
+
secondary_hue="yellow",
|
| 312 |
+
),
|
| 313 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
|
| 315 |
if __name__ == "__main__":
|
| 316 |
demo.launch()
|