Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| from sentence_transformers import SentenceTransformer | |
| import torch | |
| import numpy as np | |
| # this client will handle making requests to the model to generate responses | |
| client = InferenceClient("google/gemma-2-2b-it") | |
| # Load and process the knowledge base text file | |
| with open("knowledge.txt", "r", encoding="utf-8") as f: | |
| knowledge_text = f.read() | |
| # Split the text into chunks (for example, by paragraphs) | |
| chunks = [chunk.strip() for chunk in knowledge_text.split("\n") if chunk.strip()] | |
| # Load an embedding model (this one is light and fast) | |
| embedder = SentenceTransformer('all-MiniLM-L6-v2') | |
| # Precompute embeddings for all chunks (as a tensor for fast similarity search) | |
| chunk_embeddings = embedder.encode(chunks, convert_to_tensor=True) | |
| def get_relevant_context(query, top_k=3): | |
| """ | |
| Compute the embedding for the query, compare it against all chunk embeddings, | |
| and return the top_k most similar chunks concatenated into a context string. | |
| """ | |
| # Compute and normalize the query embedding | |
| query_embedding = embedder.encode(query, convert_to_tensor=True) | |
| query_embedding = query_embedding / query_embedding.norm() | |
| # Normalize chunk embeddings along the embedding dimension | |
| norm_chunk_embeddings = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True) | |
| # Compute cosine similarity between the query and each chunk | |
| similarities = torch.matmul(norm_chunk_embeddings, query_embedding) | |
| # Get the indices of the top_k most similar chunks | |
| top_k_indices = torch.topk(similarities, k=top_k).indices.cpu().numpy() | |
| # Concatenate the top chunks into a single context string | |
| context = "\n\n".join([chunks[i] for i in top_k_indices]) | |
| return context | |
| def respond(message, history): | |
| system_message = "You are a kitchen sous chef. You always respond with a knowledgeable and upbeat attitude! Do not answer questions unrelated to cooking. If the user asks an unrelated question, tell them you can only answer questions about cooking and do not answer their question." | |
| # initialize a list of dictionaries to store the messages | |
| messages = [{"role": "system", | |
| "content": system_message}] | |
| if history: | |
| for user_msg, assistant_msg in history: | |
| messages.append({"role": "user", "content": user_msg}) | |
| messages.append({"role": "assistant", "content": assistant_msg}) | |
| # add the current user's message to the messages list | |
| messages.append({"role": "user", "content": message}) | |
| # makes the chat completion API call, | |
| # sending the messages and other parameters to the model | |
| # implements streaming, where one word/token appears at a time | |
| response = "" | |
| # iterate through each message in the method | |
| try: | |
| for message in client.chat_completion( | |
| messages, | |
| max_tokens=150, | |
| temperature=0.8, | |
| stream=True | |
| ): | |
| # add the tokens to the output content | |
| token = message.choices[0].delta.content # capture the most recent token | |
| response += token # Add it to the response | |
| yield response # yield the response | |
| except Exception as e: | |
| print(f"An error occurred: {e}") | |
| # Create the Gradio chat interface (using Gradio's ChatInterface) | |
| title = "# SousChef AI 🍳" | |
| topics = """ | |
| ### I'm here to help you level up in the kitchen! | |
| If you're not sure where to start, try using one of the messages below: | |
| - How should I cook steak if I want it medium-rare? | |
| - What's a good recipe to make for a vegetarian potluck? | |
| - I'm making cookies and don't have baking soda, help! | |
| - How can I chop onions without crying? | |
| - Give me some dishes that combine Indian and Mediterranean cuisine. | |
| - How do I soft boil an egg? | |
| """ | |
| disclaimer = "SousChef AI is only designed to provide recommendations on how to prepare and cook different recipes. It is not responsible for any kitchen disasters or bad-tasting meals, that's all you chef!" | |
| with gr.Blocks() as chatbot: | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.Markdown(title) | |
| gr.Markdown(topics) | |
| with gr.Column(scale=2): | |
| gr.ChatInterface(respond, | |
| type="messages", | |
| examples = ["What are some cooking tips for beginners?", "What toppings should I add to my ramen?", "What's the best recipe for homemade pizza dough?"], | |
| theme= 'shivi/calm_seafoam' | |
| ) | |
| with gr.Row(): | |
| gr.Markdown(disclaimer) | |
| chatbot.launch(debug=True) |