Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,81 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
-
with gr.Blocks() as demo:
|
| 64 |
-
with gr.Sidebar():
|
| 65 |
-
gr.LoginButton()
|
| 66 |
-
chatbot.render()
|
| 67 |
-
|
| 68 |
-
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import chromadb
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Get API key from Hugging Face secrets
|
| 8 |
+
GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY')
|
| 9 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 10 |
|
| 11 |
+
# Load models (cached)
|
| 12 |
+
print("Loading embedding model...")
|
| 13 |
+
embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 14 |
+
print("Loading database...")
|
| 15 |
+
chroma_client = chromadb.PersistentClient(path="./deadcells_db_free")
|
| 16 |
+
collection = chroma_client.get_collection(name="deadcells_wiki")
|
| 17 |
+
print("Ready!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Find Gemini model
|
| 20 |
+
available_model = 'gemini-1.5-flash-latest'
|
| 21 |
|
| 22 |
+
def get_embedding(text):
|
| 23 |
+
return embedding_model.encode(text).tolist()
|
| 24 |
|
| 25 |
+
def chat(message, history):
|
| 26 |
+
"""Handle chat messages"""
|
| 27 |
+
|
| 28 |
+
# Search wiki
|
| 29 |
+
question_embedding = get_embedding(message)
|
| 30 |
+
results = collection.query(
|
| 31 |
+
query_embeddings=[question_embedding],
|
| 32 |
+
n_results=5
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
relevant_chunks = results['documents'][0]
|
| 36 |
+
sources = results['metadatas'][0]
|
| 37 |
+
|
| 38 |
+
if not relevant_chunks:
|
| 39 |
+
return "I couldn't find any relevant information in the wiki."
|
| 40 |
+
|
| 41 |
+
# Build context
|
| 42 |
+
context = "\n\n---\n\n".join(relevant_chunks)
|
| 43 |
+
|
| 44 |
+
# Ask Gemini
|
| 45 |
+
model = genai.GenerativeModel(available_model)
|
| 46 |
+
prompt = f"""You are a Dead Cells expert. Answer using ONLY the provided wiki content.
|
| 47 |
|
| 48 |
+
Wiki Content:
|
| 49 |
+
{context}
|
| 50 |
|
| 51 |
+
Question: {message}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
Answer:"""
|
| 54 |
+
|
| 55 |
+
response = model.generate_content(prompt)
|
| 56 |
+
answer = response.text
|
| 57 |
+
|
| 58 |
+
# Add sources
|
| 59 |
+
source_list = "\n\n📚 **Sources:** " + ", ".join([s['source'] for s in sources[:3]])
|
| 60 |
+
|
| 61 |
+
return answer + source_list
|
| 62 |
|
| 63 |
+
# Create Gradio interface
|
| 64 |
+
demo = gr.ChatInterface(
|
| 65 |
+
fn=chat,
|
| 66 |
+
title="🎮 Dead Cells Wiki Bot",
|
| 67 |
+
description="Ask me anything about Dead Cells! I answer using the official wiki.",
|
| 68 |
+
examples=[
|
| 69 |
+
"What achievements are there for beating bosses?",
|
| 70 |
+
"Tell me about the Hand of the King",
|
| 71 |
+
"How does malaise work?",
|
| 72 |
+
"What are boss stem cells?",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
],
|
| 74 |
+
theme="soft",
|
| 75 |
+
retry_btn=None,
|
| 76 |
+
undo_btn=None,
|
| 77 |
+
clear_btn="Clear Chat"
|
| 78 |
)
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
if __name__ == "__main__":
|
| 81 |
+
demo.launch()
|