SilverWulf212 commited on
Commit
2a64928
·
verified ·
1 Parent(s): 6f2bb6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -35
app.py CHANGED
@@ -4,46 +4,80 @@ 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}
@@ -53,29 +87,18 @@ Question: {message}
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()
 
4
  import google.generativeai as genai
5
  import os
6
 
7
+ # Get API key
8
  GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY')
9
  genai.configure(api_key=GOOGLE_API_KEY)
10
 
11
+ # Load models
12
  print("Loading embedding model...")
13
  embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
14
+ print("Checking database...")
15
+
16
+ # Create or load database
17
  chroma_client = chromadb.PersistentClient(path="./deadcells_db_free")
 
 
18
 
19
+ try:
20
+ collection = chroma_client.get_collection(name="deadcells_wiki")
21
+ print(f"Database loaded! {collection.count()} chunks available")
22
+ except:
23
+ print("Database not found! Building from wiki files...")
24
+
25
+ # Create collection
26
+ collection = chroma_client.create_collection(name="deadcells_wiki")
27
+
28
+ # Load wiki files
29
+ import glob
30
+ wiki_files = glob.glob("wiki_content/*.txt")
31
+
32
+ if not wiki_files:
33
+ raise Exception("No wiki files found! Upload wiki_content folder")
34
+
35
+ chunk_id = 0
36
+ for filepath in wiki_files:
37
+ filename = os.path.basename(filepath)
38
+ with open(filepath, 'r', encoding='utf-8') as f:
39
+ content = f.read()
40
+
41
+ lines = content.split('\n')
42
+ url = lines[0].replace('URL: ', '') if lines[0].startswith('URL:') else ''
43
+ text = '\n'.join(lines[2:])
44
+
45
+ # Simple chunking
46
+ words = text.split()
47
+ for i in range(0, len(words), 800):
48
+ chunk = ' '.join(words[i:i + 1000])
49
+ if len(chunk.split()) < 50:
50
+ continue
51
+
52
+ embedding = embedding_model.encode(chunk).tolist()
53
+
54
+ collection.add(
55
+ documents=[chunk],
56
+ embeddings=[embedding],
57
+ metadatas=[{"source": filename, "url": url, "chunk_index": i}],
58
+ ids=[f"chunk-{chunk_id}"]
59
+ )
60
+ chunk_id += 1
61
+
62
+ print(f"Database created! {chunk_id} chunks indexed")
63
+
64
  available_model = 'gemini-1.5-flash-latest'
65
 
66
  def get_embedding(text):
67
  return embedding_model.encode(text).tolist()
68
 
69
  def chat(message, history):
 
 
 
70
  question_embedding = get_embedding(message)
71
+ results = collection.query(query_embeddings=[question_embedding], n_results=5)
 
 
 
72
 
73
  relevant_chunks = results['documents'][0]
 
 
74
  if not relevant_chunks:
75
  return "I couldn't find any relevant information in the wiki."
76
 
 
77
  context = "\n\n---\n\n".join(relevant_chunks)
78
 
 
79
  model = genai.GenerativeModel(available_model)
80
+ prompt = f"""You are a Dead Cells expert. Answer using ONLY the wiki content provided.
81
 
82
  Wiki Content:
83
  {context}
 
87
  Answer:"""
88
 
89
  response = model.generate_content(prompt)
90
+ return response.text
 
 
 
 
 
91
 
 
92
  demo = gr.ChatInterface(
93
  fn=chat,
94
  title="🎮 Dead Cells Wiki Bot",
95
+ description="Ask me anything about Dead Cells!",
96
  examples=[
97
+ "What achievements are there?",
98
+ "Tell me about bosses",
99
  "How does malaise work?",
 
100
  ],
101
+ theme="soft"
 
 
 
102
  )
103
 
104
+ demo.launch()