Spaces:
Sleeping
Sleeping
code for cleaning data
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
import json
|
| 5 |
+
import numpy as np
|
| 6 |
+
import faiss
|
| 7 |
+
from sentence_transformers import SentenceTransformer
|
| 8 |
+
from bs4 import BeautifulSoup #help clean up the HTML tags in my JSON data
|
| 9 |
+
|
| 10 |
+
hf_token = os.getenv("HF_Token")
|
| 11 |
+
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct", token=hf_token)
|
| 12 |
+
embed_model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 13 |
+
|
| 14 |
+
def preprocess_text(text):
|
| 15 |
+
cleaned_text = text.strip()
|
| 16 |
+
chunks = []
|
| 17 |
+
sentences = cleaned_text.split("\n")
|
| 18 |
+
for i in sentences:
|
| 19 |
+
chunks.extend(i.split(". "))
|
| 20 |
+
|
| 21 |
+
cleaned_chunks = []
|
| 22 |
+
|
| 23 |
+
for chunk in chunks:
|
| 24 |
+
chunk = chunk.strip()
|
| 25 |
+
if len(chunk) > 0:
|
| 26 |
+
cleaned_chunks.append(chunk)
|
| 27 |
+
|
| 28 |
+
return cleaned_chunks
|
| 29 |
+
|
| 30 |
+
def prepare_docs():
|
| 31 |
+
with open('spots.json', 'r') as f:
|
| 32 |
+
raw_data = json.load(f)
|
| 33 |
+
|
| 34 |
+
all_processed_chunks = []
|
| 35 |
+
|
| 36 |
+
for item in raw_data:
|
| 37 |
+
soup = BeautifulSoup(item['popup'], 'html.parser')
|
| 38 |
+
raw_html_text = soup.get_text(separator=" ")
|
| 39 |
+
|
| 40 |
+
chunks = preprocess_text(raw_html_text)
|
| 41 |
+
|
| 42 |
+
for chunk in chunks:
|
| 43 |
+
all_processed_chunks.append(chunk)
|
| 44 |
+
|
| 45 |
+
return all_processed_chunks
|
| 46 |
+
|
| 47 |
+
processed_data = prepare_docs()
|
| 48 |
+
|
| 49 |
+
embeddings = embed_model.encode(processed_data)
|
| 50 |
+
index = faiss.IndexFlatL2(embeddings.shape[1])
|
| 51 |
+
index.add(np.array(embeddings).astype('float32'))
|
| 52 |
+
|
| 53 |
+
def retrieve(query, k=3):
|
| 54 |
+
query_vec = embed_model.encode([query])
|
| 55 |
+
distances, indices = index.search(np.array(query_vec).astype('float32'), k)
|
| 56 |
+
return [processed_data[i] for i in indices[0]]
|
| 57 |
+
|
| 58 |
+
def respond(message, history):
|
| 59 |
+
retrieved_info = retrieve(message)
|
| 60 |
+
context = "\n- ".join(retrieved_info)
|
| 61 |
+
|
| 62 |
+
system_prompt = f"""You are 'CityScout', a friendly guide to unique hangout spots.
|
| 63 |
+
Use the following verified facts from our database to help the user:
|
| 64 |
+
- {context}
|
| 65 |
+
If you find a match, describe it enthusiastically! If not, help them brainstorm based on their interests."""
|
| 66 |
+
#use the processed data in the role prompt
|
| 67 |
+
|
| 68 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 69 |
+
for user_msg, assistant_msg in history:
|
| 70 |
+
if user_msg: messages.append({"role": "user", "content": user_msg})
|
| 71 |
+
if assistant_msg: messages.append({"role": "assistant", "content": assistant_msg})
|
| 72 |
+
|
| 73 |
+
messages.append({"role": "user", "content": message})
|
| 74 |
+
|
| 75 |
+
response = ""
|
| 76 |
+
|
| 77 |
+
for chunk in client.chat_completion(
|
| 78 |
+
messages,
|
| 79 |
+
max_tokens=500,
|
| 80 |
+
temperature=0.7,
|
| 81 |
+
top_p=0.9,
|
| 82 |
+
stream=True
|
| 83 |
+
):
|
| 84 |
+
token = chunk.choices[0].delta.content
|
| 85 |
+
if token:
|
| 86 |
+
response += token
|
| 87 |
+
yield response
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
chatbot = gr.ChatInterface(respond,
|
| 91 |
+
title="CityScout: Unique Spot Finder",
|
| 92 |
+
description="Tell me your city and interests to find a cool spot!")
|
| 93 |
+
|
| 94 |
+
chatbot.launch()
|