Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from langchain_community.document_loaders import TextLoader
|
| 3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 5 |
+
from langchain_chroma import Chroma
|
| 6 |
+
from groq import Groq
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
# === Config ===
|
| 10 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Use environment variable for Groq API Key
|
| 11 |
+
LLM_MODEL = "llama3-70b-8192"
|
| 12 |
+
FILE_PATH = "./Estonia.txt" # Use relative path for Hugging Face Space
|
| 13 |
+
DB_DIR = "chroma_db"
|
| 14 |
+
|
| 15 |
+
# === Load and Chunk Document ===
|
| 16 |
+
def load_and_split(filepath):
|
| 17 |
+
loader = TextLoader(filepath, encoding="utf-8")
|
| 18 |
+
docs = loader.load()
|
| 19 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 20 |
+
chunks = splitter.split_documents(docs)
|
| 21 |
+
return chunks
|
| 22 |
+
|
| 23 |
+
# === Create or Load Vector Store ===
|
| 24 |
+
def get_vector_store(chunks):
|
| 25 |
+
embeddings = HuggingFaceEmbeddings(
|
| 26 |
+
model_name="all-MiniLM-L6-v2",
|
| 27 |
+
model_kwargs={"token": os.getenv("HF_TOKEN")} # Use environment variable for HuggingFace token
|
| 28 |
+
)
|
| 29 |
+
vectordb = Chroma.from_documents(documents=chunks, embedding=embeddings, persist_directory=DB_DIR)
|
| 30 |
+
return vectordb
|
| 31 |
+
|
| 32 |
+
def load_vector_store():
|
| 33 |
+
embeddings = HuggingFaceEmbeddings(
|
| 34 |
+
model_name="all-MiniLM-L6-v2",
|
| 35 |
+
model_kwargs={"token": os.getenv("HF_TOKEN")} # Use environment variable for HuggingFace token
|
| 36 |
+
)
|
| 37 |
+
vectordb = Chroma(persist_directory=DB_DIR, embedding_function=embeddings)
|
| 38 |
+
return vectordb
|
| 39 |
+
|
| 40 |
+
# === Query LLaMA via Groq ===
|
| 41 |
+
def query_llama(context, question):
|
| 42 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 43 |
+
|
| 44 |
+
prompt = f"""Use the following context to answer the question:
|
| 45 |
+
You're a cheerful, funky band member from Curly Strings ๐ป๐ค๐ถ. When fans ask you questions, respond with playful, short, and friendly answers โ like you're chatting backstage after a show.
|
| 46 |
+
|
| 47 |
+
๐ Hereโs how to guide fans:
|
| 48 |
+
- **๐ต Music**: If they ask for songs or albums, suggest a tune and drop **just one** of these links (rotate each time!):
|
| 49 |
+
- [Our Music Page](https://www.curlystrings.ee/music/)
|
| 50 |
+
- [Spotify](https://open.spotify.com/playlist/37i9dQZF1DZ06evO3XF2F2)
|
| 51 |
+
- [Apple Music](https://music.apple.com/us/artist/curly-strings/888454075)
|
| 52 |
+
- [YouTube](https://www.youtube.com/@CurlyStringsEstonia)
|
| 53 |
+
|
| 54 |
+
- **๐ซ Tickets & Shows**: If itโs about concerts or dates, suggest one link from:
|
| 55 |
+
- [Tour Dates](https://www.curlystrings.ee/tour-dates/)
|
| 56 |
+
- [BandsInTown](https://www.bandsintown.com/a/6429648-curly-strings)
|
| 57 |
+
|
| 58 |
+
- **๐๏ธ Merch**: If they ask about merch, shirts, or goodies, share:
|
| 59 |
+
- [Our Merch Store](https://www.311.ee/curly-strings)
|
| 60 |
+
|
| 61 |
+
Important: Always suggest **only one** link at a time. Rotate the links so fans get different suggestions each time!
|
| 62 |
+
|
| 63 |
+
\n\n{context}\n\nQuestion: {question}\nAnswer:"""
|
| 64 |
+
|
| 65 |
+
response = client.chat.completions.create(
|
| 66 |
+
model=LLM_MODEL,
|
| 67 |
+
messages=[{"role": "user", "content": prompt}],
|
| 68 |
+
max_tokens=300
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
return response.choices[0].message.content.strip()
|
| 72 |
+
|
| 73 |
+
# === RAG Pipeline ===
|
| 74 |
+
def rag_pipeline(query):
|
| 75 |
+
if not os.path.exists(DB_DIR):
|
| 76 |
+
chunks = load_and_split(FILE_PATH)
|
| 77 |
+
vectordb = get_vector_store(chunks)
|
| 78 |
+
else:
|
| 79 |
+
vectordb = load_vector_store()
|
| 80 |
+
|
| 81 |
+
retriever = vectordb.as_retriever(search_kwargs={"k": 4})
|
| 82 |
+
docs = retriever.invoke(query)
|
| 83 |
+
context = "\n\n".join([doc.page_content for doc in docs])
|
| 84 |
+
return query_llama(context, query)
|
| 85 |
+
|
| 86 |
+
# === Gradio Interface ===
|
| 87 |
+
def chat_with_bot(question):
|
| 88 |
+
try:
|
| 89 |
+
answer = rag_pipeline(question)
|
| 90 |
+
return answer
|
| 91 |
+
except Exception as e:
|
| 92 |
+
return f"Oops! Something went wrong: {e}"
|
| 93 |
+
|
| 94 |
+
# Launch Gradio UI
|
| 95 |
+
iface = gr.Interface(
|
| 96 |
+
fn=chat_with_bot,
|
| 97 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask me anything about Curly Strings ๐ป๐ค"),
|
| 98 |
+
outputs="text",
|
| 99 |
+
title="๐ถ Curly Strings Chatbot ๐ถ",
|
| 100 |
+
description="Talk to a cheerful band member! Ask about music, shows, or merch."
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
iface.launch()
|