File size: 1,768 Bytes
4777736
cece503
c583b68
cece503
 
 
4777736
57c34bf
4777736
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cece503
 
 
4777736
cece503
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4777736
 
 
 
 
 
 
cece503
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

import gradio as gr
from huggingface_hub import InferenceClient
from sentence_transformers import SentenceTransformer
import torch

# Load knowledge
with open("food_recipe.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 chatbot:
                    gr.ChatInterface(
                        fn=respond,
                        type="messages",
                    )
chatbot.launch()