bitebot_app / app.py
aashnaj's picture
title
7d09e3e verified
raw
history blame
2.82 kB
import gradio as gr
from huggingface_hub import InferenceClient
from sentence_transformers import SentenceTransformer
import torch
# Load knowledge
with open("recipesplease.txt", "r", encoding="utf-8") as file:
knowledge = file.read()
cleaned_chunks = [chunk.strip() for chunk in knowledge.strip().split("\n") if chunk.strip()]
model = SentenceTransformer('all-MiniLM-L6-v2')
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_normalized = query_embedding / query_embedding.norm()
similarities = torch.matmul(chunk_embeddings, query_embedding_normalized)
top_indices = torch.topk(similarities, k=5).indices.tolist()
return [cleaned_chunks[i] for i in top_indices]
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
def respond(message, history):
response = ""
top_chunks = get_top_chunks(message)
context = "\n".join(top_chunks)
messages = [
{
"role": "system",
"content": f"You are a friendly chatbot that responds to the user with this context {context}"
}
]
if history:
messages.extend(history)
messages.append({"role": "user", "content": message})
stream = client.chat_completion(
messages,
max_tokens=300,
temperature=1.2,
stream=True,
)
for message in stream:
token = message.choices[0].delta.content
if token is not None:
response += token
yield response
with gr.Blocks() as demo:
gr.Markdown("## 🧠🍴 The BiteBot")
theme = gr.themes.Monochrome(
primary_hue="orange",
secondary_hue="zinc",
neutral_hue=gr.themes.Color(c100="rgba(255, 227.4411088400613, 206.9078947368421, 1)", c200="rgba(255, 229.53334184977007, 218.0921052631579, 1)", c300="rgba(255, 234.91658150229947, 213.6184210526316, 1)", c400="rgba(189.603125, 154.41663986650488, 133.88641721491229, 1)", c50="#f3d1bbff", c500="rgba(170.2125, 139.18781968574348, 118.70082236842106, 1)", c600="rgba(193.32187499999998, 129.35648241888094, 111.07528782894737, 1)", c700="rgba(184.13125000000002, 141.9707339039346, 106.60230263157897, 1)", c800="rgba(156.06796875, 104.12209005333418, 69.81988075657894, 1)", c900="rgba(156.39999999999998, 117.22008175779253, 80.2578947368421, 1)", c950="rgba(158.43203125, 125.1788770279765, 97.28282620614036, 1)"),
text_size="sm",
spacing_size="md",
radius_size="sm",
).set(
body_background_fill='*primary_50',
body_background_fill_dark='*primary_50'
)
with gr.Blocks(theme=theme) as chatbot:
gr.ChatInterface(
fn=respond,
type="messages",
)
chatbot.launch()