try
Browse files
app.py
CHANGED
|
@@ -1,114 +1,60 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import random
|
| 3 |
import os
|
| 4 |
from huggingface_hub import InferenceClient
|
| 5 |
from sentence_transformers import SentenceTransformer
|
| 6 |
import torch
|
| 7 |
|
|
|
|
| 8 |
with open("knowledge.txt", "r", encoding="utf-8") as file:
|
| 9 |
-
|
| 10 |
-
# opens the text, saves as "file"
|
| 11 |
-
# reads the text and saves as water_cycle_text variable
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
cleaned_text = recent.strip()
|
| 16 |
-
# cleaning up the text
|
| 17 |
-
chunks = cleaned_text.split("\n")
|
| 18 |
-
# seperating the text into one sentence pieces
|
| 19 |
-
cleaned_chunks = []
|
| 20 |
-
# creating an empty list to put the cleaned chunks in
|
| 21 |
-
|
| 22 |
-
for chunk in chunks:
|
| 23 |
-
stripped_chunk = chunk.strip()
|
| 24 |
-
if stripped_chunk:
|
| 25 |
-
cleaned_chunks.append(stripped_chunk)
|
| 26 |
-
# loop through chunks and add not empty chunks to cleaned_chunks list
|
| 27 |
-
|
| 28 |
|
|
|
|
| 29 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
# encode the model, pass through my cleaned chunks and convert to vector embeddings (not arrays)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
def get_top_chunks(query):
|
| 36 |
-
# create my function taking query as parameter
|
| 37 |
-
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 38 |
-
# encode query to vector embedding for comparison
|
| 39 |
-
query_embedding_normalized = query_embedding / query_embedding.norm()
|
| 40 |
-
# normalize my query to 1; allows for comparison of meaning
|
| 41 |
-
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
|
| 42 |
-
# normailizing chunks for comparison of meaning
|
| 43 |
-
|
| 44 |
-
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)
|
| 45 |
-
|
| 46 |
-
# using matmul (matrix multiplication method) to compare query to chunks
|
| 47 |
-
top_indices = torch.topk(similarities, k=3).indices
|
| 48 |
-
|
| 49 |
-
# get the indices of the chunks that are most similar to query
|
| 50 |
-
|
| 51 |
-
top_chunks = []
|
| 52 |
-
|
| 53 |
-
for i in top_indices:
|
| 54 |
-
chunk = chunks[i]
|
| 55 |
-
# for each index number in top_indices, get back the text
|
| 56 |
-
top_chunks.append(chunk)
|
| 57 |
-
# values of each index number is added to top_chunks
|
| 58 |
-
return top_chunks
|
| 59 |
-
|
| 60 |
client = InferenceClient(
|
| 61 |
-
model='Qwen/Qwen2.5-72B-Instruct'
|
| 62 |
-
#token
|
| 63 |
-
)
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
def
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
response = ""
|
| 78 |
-
for
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
):
|
| 87 |
-
token = message.choices[0].delta.content
|
| 88 |
-
response += token
|
| 89 |
-
yield response
|
| 90 |
-
#print(response["choices"][0]["message"]["content"].strip())
|
| 91 |
-
|
| 92 |
-
#yield response["choices"][0]["message"]["content"].strip()
|
| 93 |
-
|
| 94 |
-
chatbot = gr.ChatInterface(respond, type='messages')
|
| 95 |
|
| 96 |
-
with gr.Blocks(theme='hmb/amethyst') as chatbot:
|
| 97 |
-
with gr.Row(equal_height=True):
|
| 98 |
-
with gr.Column(scale=10):
|
| 99 |
-
gr.Markdown(
|
| 100 |
-
"""
|
| 101 |
-
# 🎁 Introducing WrapIT!
|
| 102 |
-
**WrapIT** helps users find personalized gift ideas and craft thoughtful card messages
|
| 103 |
-
by inputting details like the recipient's interests, celebration type, and budget.
|
| 104 |
-
|
| 105 |
-
✨ *All you have to do is wrap it.*
|
| 106 |
-
"""
|
| 107 |
-
)
|
| 108 |
-
#gr.chatInterface()
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
chatbot = gr.Chatbot()
|
| 113 |
msg = gr.Textbox(placeholder="Say hi to WrapIT here!", label="Message")
|
| 114 |
send = gr.Button("Send")
|
|
@@ -116,5 +62,4 @@ with gr.Blocks(theme='hmb/amethyst') as chatbot:
|
|
| 116 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 117 |
send.click(respond, [msg, chatbot], [msg, chatbot])
|
| 118 |
|
| 119 |
-
|
| 120 |
-
chatbot.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import os
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
import torch
|
| 6 |
|
| 7 |
+
# Load and prepare knowledge base
|
| 8 |
with open("knowledge.txt", "r", encoding="utf-8") as file:
|
| 9 |
+
recent = file.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
chunks = [chunk.strip() for chunk in recent.strip().split("\n") if chunk.strip()]
|
| 12 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 13 |
+
chunk_embeddings = model.encode(chunks, convert_to_tensor=True)
|
| 14 |
|
| 15 |
+
# Hugging Face Inference Client (add token if required)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
client = InferenceClient(
|
| 17 |
+
model='Qwen/Qwen2.5-72B-Instruct',
|
| 18 |
+
# token=os.getenv("HF_TOKEN")
|
| 19 |
+
)
|
| 20 |
|
| 21 |
+
# Top similar chunks
|
| 22 |
+
def get_top_chunks(query):
|
| 23 |
+
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 24 |
+
query_embedding /= query_embedding.norm()
|
| 25 |
+
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
|
| 26 |
+
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding)
|
| 27 |
+
top_indices = torch.topk(similarities, k=3).indices
|
| 28 |
+
return [chunks[i] for i in top_indices]
|
| 29 |
+
|
| 30 |
+
# Main response function
|
| 31 |
+
def respond(user_message, chat_history):
|
| 32 |
+
gift_ideas = get_top_chunks(user_message)
|
| 33 |
+
messages = [{"role": "system", "content": f"You are helpful and give 5 gift ideas with prices. Use this database: {gift_ideas}"}]
|
| 34 |
+
for user, bot in chat_history:
|
| 35 |
+
messages.append({"role": "user", "content": user})
|
| 36 |
+
messages.append({"role": "assistant", "content": bot})
|
| 37 |
+
messages.append({"role": "user", "content": user_message})
|
| 38 |
|
| 39 |
response = ""
|
| 40 |
+
for part in client.chat_completion(messages, max_tokens=500):
|
| 41 |
+
token = part.choices[0].delta.content
|
| 42 |
+
if token:
|
| 43 |
+
response += token
|
| 44 |
+
|
| 45 |
+
chat_history.append((user_message, response))
|
| 46 |
+
return "", chat_history
|
| 47 |
+
|
| 48 |
+
# Gradio app
|
| 49 |
+
with gr.Blocks(theme='hmb/amethyst') as demo:
|
| 50 |
+
gr.Markdown("""
|
| 51 |
+
# 🎁 Introducing WrapIT!
|
| 52 |
+
**WrapIT** helps users find personalized gift ideas and craft thoughtful card messages**
|
| 53 |
+
by inputting details like the recipient's interests, celebration type, and budget.
|
| 54 |
|
| 55 |
+
✨ *All you have to do is wrap it.*
|
| 56 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
chatbot = gr.Chatbot()
|
| 59 |
msg = gr.Textbox(placeholder="Say hi to WrapIT here!", label="Message")
|
| 60 |
send = gr.Button("Send")
|
|
|
|
| 62 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 63 |
send.click(respond, [msg, chatbot], [msg, chatbot])
|
| 64 |
|
| 65 |
+
demo.launch()
|
|
|