Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,59 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
|
| 6 |
|
| 7 |
def respond(message, history):
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
if history:
|
| 10 |
messages.extend(history)
|
| 11 |
messages.append({"role": "user", "content": message})
|
| 12 |
|
| 13 |
-
# response = client.chat_completion(
|
| 14 |
-
# messages,
|
| 15 |
-
# max_tokens=100,
|
| 16 |
-
# temperature = 1.5,
|
| 17 |
-
# top_p = 0.34
|
| 18 |
-
# )
|
| 19 |
response = ""
|
| 20 |
for msg in client.chat_completion(messages, stream=True):
|
| 21 |
token = msg.choices[0].delta.content
|
| 22 |
if token is not None:
|
| 23 |
response += token
|
| 24 |
-
yield response
|
| 25 |
-
|
| 26 |
-
chatbot = gr.ChatInterface(respond, title="my bot")
|
| 27 |
-
|
| 28 |
|
|
|
|
| 29 |
chatbot.launch(debug=True)
|
|
|
|
| 1 |
+
import torch
|
| 2 |
import gradio as gr
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
|
| 6 |
+
with open("travel_knowledge_base.txt", "r", encoding="utf-8") as file:
|
| 7 |
+
travel_text = file.read()
|
| 8 |
+
|
| 9 |
+
def preprocess_text(text):
|
| 10 |
+
cleaned_text = text.strip()
|
| 11 |
+
chunks = cleaned_text.split("\n")
|
| 12 |
+
cleaned_chunks = []
|
| 13 |
+
for chunk in chunks:
|
| 14 |
+
chunk = chunk.strip()
|
| 15 |
+
if chunk != "":
|
| 16 |
+
cleaned_chunks.append(chunk)
|
| 17 |
+
return cleaned_chunks
|
| 18 |
+
|
| 19 |
+
cleaned_chunks = preprocess_text(travel_text)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 23 |
+
|
| 24 |
+
def create_embeddings(text_chunks):
|
| 25 |
+
chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True)
|
| 26 |
+
return chunk_embeddings
|
| 27 |
+
|
| 28 |
+
chunk_embeddings = create_embeddings(cleaned_chunks)
|
| 29 |
+
|
| 30 |
+
def get_top_chunks(query, chunk_embeddings, text_chunks):
|
| 31 |
+
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 32 |
+
query_embedding_normalized = query_embedding / query_embedding.norm()
|
| 33 |
+
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
|
| 34 |
+
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
|
| 35 |
+
top_indices = torch.topk(similarities, k=3).indices
|
| 36 |
+
top_chunks = []
|
| 37 |
+
for index in top_indices:
|
| 38 |
+
top_chunks.append(text_chunks[index])
|
| 39 |
+
return top_chunks
|
| 40 |
|
| 41 |
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
|
| 42 |
|
| 43 |
def respond(message, history):
|
| 44 |
+
top_chunks = get_top_chunks(message, chunk_embeddings, cleaned_chunks)
|
| 45 |
+
context = "\n\n".join(top_chunks)
|
| 46 |
+
messages = [{"role": "system", "content": f"You are a travel advisor. Give information about transportation, food, hotels, landmarks, and more. \n{context}"}]
|
| 47 |
if history:
|
| 48 |
messages.extend(history)
|
| 49 |
messages.append({"role": "user", "content": message})
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
response = ""
|
| 52 |
for msg in client.chat_completion(messages, stream=True):
|
| 53 |
token = msg.choices[0].delta.content
|
| 54 |
if token is not None:
|
| 55 |
response += token
|
| 56 |
+
yield response
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
chatbot = gr.ChatInterface(respond, type="messages")
|
| 59 |
chatbot.launch(debug=True)
|