capstoneChatbot / app.py
denizd's picture
Update app.py
0156830 verified
import gradio as gr
import torch
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("all-MiniLM-L6-v2")
rochester_food_places_text = """
Pudgy’s Pizza is a casual spot near the University of Rochester known for large slices and cheap prices.
Cam’s Pizzeria offers NY-style thin crust pizza close to campus.
Pontillo’s Pizzeria is a reliable chain that serves traditional pizza with delivery options.
Yummy Garden serves affordable Chinese food and delivers near the university.
Thai Mii Up is a popular Thai restaurant near the University of Rochester known for Pad Thai and curry.
Flavors of Asia blends Chinese, Thai, and Vietnamese cuisines close to the university.
The Distillery is a lively sports bar with burgers and wings near the University of Rochester.
Bill Gray’s is a local chain known for classic American burgers and milkshakes.
Tap & Mallet offers gourmet burgers and craft beers just a short distance from campus.
John’s Tex Mex serves burritos, tacos, and vegan options near the university.
Chipotle Mexican Grill provides fast-casual customizable burritos and bowls near the campus.
El Latino Restaurant serves authentic Mexican and Latin American dishes close to the university.
Amiel’s Submarine is known for subs and pasta near the University of Rochester.
Rocco is an upscale Italian restaurant offering fine dining near the campus.
Dinosaur Bar-B-Que serves BBQ with Southern Italian-inspired sides not far from the university.
The Red Fern is a 100% vegan café with sandwiches, smoothies, and desserts close to U of R.
Just Juice 4 Life offers smoothies and organic bowls within reach of the university.
Voula’s Greek Sweets is a vegetarian-friendly café offering Greek dishes near the University of Rochester.
Java’s Café is a student-favorite coffee shop with live music and study space near campus.
Boulder Coffee is a cozy café with local art and a relaxed vibe near the university.
Starbucks in College Town is a convenient spot for coffee and studying near the University of Rochester.
"""
cleaned_text = rochester_food_places_text.strip()
chunks = cleaned_text.split("\n")
cleaned_chunks = [chunk.strip() for chunk in chunks if chunk.strip()]
chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
def get_top_chunks(query):
query_embedding = model.encode(query, convert_to_tensor=True)
query_embedding = query_embedding / query_embedding.norm(dim=0, keepdim=True)
normalized_chunks = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
similarities = torch.matmul(normalized_chunks, query_embedding.T).squeeze()
top_k = min(3, similarities.size(0))
top_indices = torch.topk(similarities, k=top_k).indices
top_chunks = [cleaned_chunks[i] for i in top_indices]
return top_chunks
def chatbot_response(message, history):
top_chunks = get_top_chunks(message)
numbered_response = "\n".join([f"{i+1}. {chunk}" for i, chunk in enumerate(top_chunks)])
return [{"responder": "chatbot", "content": numbered_response}]
chatbot = gr.ChatInterface(fn=chatbot_response, title="RAG Chatbot")
chatbot.launch()