Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import random | |
| from huggingface_hub import InferenceClient | |
| from sentence_transformers import SentenceTransformer | |
| import torch | |
| with open("knowledge.txt", "r", encoding="utf-8") as file: | |
| recent = file.read() | |
| cleaned_text = recent.strip() | |
| chunks = cleaned_text.split("\n") | |
| cleaned_chunks = [] | |
| for chunk in chunks: | |
| stripped_chunk = chunk.strip() | |
| if stripped_chunk: | |
| cleaned_chunks.append(stripped_chunk) | |
| 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() | |
| chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True) | |
| similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized) | |
| top_indices = torch.topk(similarities, k=3).indices | |
| top_chunks = [] | |
| for i in top_indices: | |
| chunk = chunks[i] | |
| top_chunks.append(chunk) | |
| return top_chunks | |
| client = InferenceClient('HuggingFaceH4/zephyr-7b-beta') | |
| def respond(message,history): | |
| gift_ideas = get_top_chunks(message) | |
| messages = [{'role': 'system', 'content': f'You give really good gift ideas and are super helpful! You also tell me the price of each item. Give me 5 gift ideas if I ask. Use the following database for gift ideas: {gift_ideas}'}] | |
| if history: | |
| messages.extend(history) | |
| messages.append({"role": "user", "content": message}) | |
| response = client.chat_completion( | |
| messages, | |
| max_tokens = 500, | |
| ) | |
| return response['choices'][0]['message']['content'].strip() | |
| with gr.Blocks(theme='hmb/amethyst') as demo: | |
| gr.Image(value="wrap_it_top_image.png", show_label=False, elem_id="top-image") | |
| gr.Markdown("## 🎁 Introducing WrapIT!") | |
| gr.Markdown("**WrapIT** helps users find personalized gift ideas and craft thoughtful card messages by inputting details like the recipient's interests, celebration type, and budget ✨ *All you have to do is wrap it.*") | |
| gr.ChatInterface( | |
| fn=respond, | |
| examples=["Best birthday gift?", "Romantic anniversary idea?", "Budget-friendly gifts?"] | |
| ) | |
| with gr.Row(): | |
| gr.HTML( | |
| """ | |
| <iframe style="border-radius:12px" | |
| src="https://open.spotify.com/embed/track/4356Typ82hUiFAynbLYbPn" | |
| width="100%" | |
| height="152" | |
| frameBorder="0" | |
| allowfullscreen="" | |
| allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" | |
| loading="lazy"> | |
| </iframe> | |
| """ | |
| ) | |
| demo.launch(debug=True, share=True) |