shradhcodes's picture
html changes
2811515 verified
Raw
History Blame Contribute Delete
4.78 kB
import gradio as gr
from huggingface_hub import InferenceClient
from sentence_transformers import SentenceTransformer
import torch
# insert the knowledge base file
with open("knowledge.txt", "r",
encoding = "utf-8") as file:
knowledge_base = file.read()
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
# chunk the text
def preprocess_text(text):
cleaned_text = text.strip()
chunks = cleaned_text.split("\n\n")
cleaned_chunks = []
for chunk in chunks:
stripped_chunk = chunk.strip()
if len(stripped_chunk) > 0:
cleaned_chunks.append(stripped_chunk)
return cleaned_chunks
cleaned_chunks = preprocess_text(knowledge_base)
# convert chunk into vector embeddings
model = SentenceTransformer('all-MiniLM-L6-v2')
def create_embeddings(text_chunks):
chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)
return chunk_embeddings
chunk_embeddings = create_embeddings(cleaned_chunks)
# similarties with query
def grab_top_chunks(query, chunk_embeddings, text_chunks):
query_embedding = model.encode(query, convert_to_tensor=True)
query_normalized = query_embedding / query_embedding.norm()
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
similarities = torch.matmul(chunk_embeddings_normalized, query_normalized)
top_indices = torch.topk(similarities, k=3).indices
top_chunks = []
for i in top_indices:
chunk = text_chunks[i]
top_chunks.append(chunk)
return top_chunks
def respond(message, history):
messages = [{"role": "system", "content": f"""You are MealMind AI, a recipe recommendation chatbot.
If anyone asks who created you, who made you, who developed you, who built you, or who your creators are, reply:
"I was created by Jinal Mehta and Vivienne Addyson as part of the Kode with Klossy project."
use the following knowledge base:
{knowledge_base}
"""
}]
if history:
messages.extend(history)
messages.append({"role": "user", "content": message})
response = client.chat_completion(
messages,
max_tokens = 1000
)
return response.choices[0].message.content.strip()
custom_css = """
.center-img {
margin: auto !important;
}
.center-img img {
object-fit: contain !important;
max-height: 200px !important;
width: auto !important;
padding: 0 !important;
border: none !important;
background: none !important;
}
"""
# --- Theme Setup ---
with gr.Blocks(theme=gr.Theme.from_hub("allenai/gradio-theme")) as demo:
# --- Main Layout Column ---
with gr.Column():
# Display the logo
image_output = gr.Image(value="mealmind.png", container=False, elem_classes="center-img")
# Display Titles and Descriptions
gr.HTML("<h1 style='text-align: center;'>Hi, I'm 🍽️ MealMind AI</h1>")
gr.HTML("<p style='text-align: center;'>Your AI-powered meal recommendation assistant. List your ingredients to get started.</p>")
# --- Chatbot Interface Section ---
examples = [
"What is a good recipe for a quick pasta dish?",
"Can you suggest a healthy breakfast without eggs?",
"How do I make a chocolate cake?"
]
chatbot = gr.ChatInterface(fn=respond, examples=examples)
# --- Resource Cards Section ---
gr.HTML("""
<div style="flex-direction: column; gap: 16px; padding: 10px 0;">
<a href="https://www.seriouseats.com/introductory-first-cookbooks" target="_blank" style="text-decoration: none; flex: 1;">
<div style="background: #f0529c; border: 2px solid #e1dad2; border-radius: 10px; padding: 20px; text-align: center; cursor: pointer; transition: transform 0.2s, box-shadow 0.2s;">
<h3 style="margin: 0; color: #e1dad2;"><b>👩‍🍳 Cookbooks for You</b></h3>
<p style="margin: 8px 0 0; color: #e1dad2;">Click the link to browse through.</p>
</div>
</a>
<a href="https://www.seriouseats.com/basic-starter-kitchen-equipment" target="_blank" style="text-decoration: none; flex: 1;">
<div style="background: #f0529c; border: 2px solid #e1dad2; border-radius: 10px; padding: 20px; text-align: center; cursor: pointer; transition: transform 0.2s, box-shadow 0.2s;">
<h3 style="margin: 0; color: #e1dad2;"><b>🍳 Kitchen Essentials</b></h3>
<p style="margin: 8px 0 0; color: #e1dad2;">Click the link to browse through.</p>
</div>
</a>
</div>
""")
# --- Launch the App ---
demo.launch(css=custom_css)