Sahabi87 commited on
Commit
dc63593
Β·
verified Β·
1 Parent(s): 526135f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -81
app.py CHANGED
@@ -1,9 +1,8 @@
1
  import os
2
  import shutil
3
-
4
  import gradio as gr
5
- from transformers import pipeline
6
 
 
7
  from langchain_community.vectorstores import Chroma
8
  from langchain_community.embeddings import SentenceTransformerEmbeddings
9
  from langchain_text_splitters import RecursiveCharacterTextSplitter
@@ -13,49 +12,55 @@ from langchain_core.documents import Document
13
  # Global State
14
  # =========================
15
  retriever = None
16
- db_path = "./chromadb"
17
 
18
  # =========================
19
- # Models (CPU Friendly)
20
  # =========================
21
  embeddings = SentenceTransformerEmbeddings(
22
  model_name="sentence-transformers/all-MiniLM-L6-v2",
23
  model_kwargs={"device": "cpu"}
24
  )
25
 
 
 
 
26
  generator = pipeline(
27
- "text2text-generation",
28
  model="google/flan-t5-small",
29
- device=-1 # CPU
30
  )
31
 
32
  # =========================
33
- # Load Hidden File
34
  # =========================
35
- def load_hidden_file(file_path="ai-geoth.txt"):
36
- try:
37
- with open(file_path, "r", encoding="utf-8") as f:
38
- return f.read()
39
- except Exception as e:
40
- print(f"⚠️ File load error: {e}")
41
- return ""
 
 
 
 
 
42
 
43
  # =========================
44
- # Build Vector Database
45
  # =========================
46
- def build_vector_db(content):
47
  global retriever
48
 
49
- if not content.strip():
50
- print("⚠️ Empty document. Vector DB not created.")
51
- return
52
-
53
  splitter = RecursiveCharacterTextSplitter(
54
- chunk_size=800,
55
  chunk_overlap=100
56
  )
57
 
58
- chunks = splitter.split_text(content)
 
 
59
 
60
  docs = [
61
  Document(
@@ -65,98 +70,81 @@ def build_vector_db(content):
65
  for i, chunk in enumerate(chunks)
66
  ]
67
 
68
- # Rebuild DB safely
69
- if os.path.exists(db_path):
70
- shutil.rmtree(db_path)
71
 
72
  db = Chroma.from_documents(
73
  docs,
74
  embeddings,
75
- persist_directory=db_path
76
  )
77
 
78
- retriever = db.as_retriever(search_kwargs={"k": 4})
79
- print(f"βœ… Vector DB built with {len(docs)} chunks")
80
 
81
  # =========================
82
- # RAG Answer Function
83
  # =========================
84
  def smart_answer(question):
85
  if retriever is None:
86
- return "⚠️ Document not loaded. Please check ai-geoth.txt."
87
 
88
- try:
89
- docs = retriever.get_relevant_documents(question)
90
 
91
- if not docs:
92
- return "❓ No relevant information found."
93
 
94
- context = "\n\n".join(
95
- d.page_content[:600] for d in docs
96
- )
97
 
98
- prompt = f"""
99
- Use ONLY the following context to answer.
100
-
101
- CONTEXT:
102
- {context}
103
-
104
- QUESTION:
105
- {question}
106
-
107
- ANSWER:
108
- """
109
 
110
- result = generator(
111
- prompt,
112
- max_new_tokens=200,
113
- do_sample=False,
114
- temperature=0.1
115
- )
116
 
117
- answer = result[0]["generated_text"]
118
- answer = answer.split("ANSWER:")[-1].strip()
119
 
120
- return answer
 
121
 
122
- except Exception as e:
123
- print(f"⚠️ Answer error: {e}")
124
- return "⚠️ Error generating answer."
125
 
126
  # =========================
127
  # Gradio UI
128
  # =========================
129
- with gr.Blocks(title="πŸ€– RAG Chatbot", theme=gr.themes.Soft()) as demo:
130
- gr.Markdown(
131
- "## πŸ€– **RAG Chatbot**\n"
132
- "*Ask questions about your hidden document.*"
133
- )
134
 
135
  chatbot = gr.Chatbot(height=500)
136
 
137
  with gr.Row():
138
- question = gr.Textbox(
139
- placeholder="Type your question and press Enter...",
140
  show_label=False
141
  )
142
- send_btn = gr.Button("Send", variant="primary")
143
-
144
- def respond(message, chat_history):
145
- reply = smart_answer(message)
146
- chat_history.append((message, reply))
147
- return "", chat_history
148
 
149
- question.submit(respond, [question, chatbot], [question, chatbot])
150
- send_btn.click(respond, [question, chatbot], [question, chatbot])
 
 
151
 
152
- # =========================
153
- # Initialize DB at Startup
154
- # =========================
155
- hidden_content = load_hidden_file()
156
- build_vector_db(hidden_content)
157
 
158
  # =========================
159
- # Launch App
160
  # =========================
161
  if __name__ == "__main__":
 
 
162
  demo.launch()
 
1
  import os
2
  import shutil
 
3
  import gradio as gr
 
4
 
5
+ from transformers import pipeline
6
  from langchain_community.vectorstores import Chroma
7
  from langchain_community.embeddings import SentenceTransformerEmbeddings
8
  from langchain_text_splitters import RecursiveCharacterTextSplitter
 
12
  # Global State
13
  # =========================
14
  retriever = None
15
+ DB_PATH = "./chromadb"
16
 
17
  # =========================
18
+ # Embeddings (CPU)
19
  # =========================
20
  embeddings = SentenceTransformerEmbeddings(
21
  model_name="sentence-transformers/all-MiniLM-L6-v2",
22
  model_kwargs={"device": "cpu"}
23
  )
24
 
25
+ # =========================
26
+ # Generator (FLAN-T5)
27
+ # =========================
28
  generator = pipeline(
29
+ task="text2text-generation",
30
  model="google/flan-t5-small",
31
+ device=-1
32
  )
33
 
34
  # =========================
35
+ # Load TXT Document
36
  # =========================
37
+ def load_hidden_file(path="ai-geoth.txt"):
38
+ if not os.path.exists(path):
39
+ raise FileNotFoundError("❌ ai-geoth.txt NOT FOUND")
40
+
41
+ with open(path, "r", encoding="utf-8") as f:
42
+ text = f.read()
43
+
44
+ if not text.strip():
45
+ raise ValueError("❌ ai-geoth.txt is EMPTY")
46
+
47
+ print(f"βœ… Loaded document ({len(text)} characters)")
48
+ return text
49
 
50
  # =========================
51
+ # Build Vector DB
52
  # =========================
53
+ def build_vector_db(text):
54
  global retriever
55
 
 
 
 
 
56
  splitter = RecursiveCharacterTextSplitter(
57
+ chunk_size=700,
58
  chunk_overlap=100
59
  )
60
 
61
+ chunks = splitter.split_text(text)
62
+
63
+ print(f"βœ… Split into {len(chunks)} chunks")
64
 
65
  docs = [
66
  Document(
 
70
  for i, chunk in enumerate(chunks)
71
  ]
72
 
73
+ if os.path.exists(DB_PATH):
74
+ shutil.rmtree(DB_PATH)
 
75
 
76
  db = Chroma.from_documents(
77
  docs,
78
  embeddings,
79
+ persist_directory=DB_PATH
80
  )
81
 
82
+ retriever = db.as_retriever(search_kwargs={"k": 3})
83
+ print("βœ… Vector DB ready")
84
 
85
  # =========================
86
+ # RAG Answer (NO SILENT FAILURES)
87
  # =========================
88
  def smart_answer(question):
89
  if retriever is None:
90
+ return "⚠️ Vector database not initialized."
91
 
92
+ docs = retriever.get_relevant_documents(question)
 
93
 
94
+ if not docs:
95
+ return "❓ No relevant text found in document."
96
 
97
+ # πŸ” DEBUG: confirm text retrieval
98
+ context = "\n".join(d.page_content for d in docs)
 
99
 
100
+ prompt = (
101
+ "Answer the question using ONLY the context below.\n\n"
102
+ f"Context:\n{context}\n\n"
103
+ f"Question:\n{question}\n\n"
104
+ "Answer:"
105
+ )
 
 
 
 
 
106
 
107
+ # ❗ NO try/except – show real errors
108
+ result = generator(
109
+ prompt,
110
+ max_new_tokens=150,
111
+ do_sample=False
112
+ )
113
 
114
+ answer = result[0]["generated_text"].strip()
 
115
 
116
+ if not answer:
117
+ return "⚠️ Model returned empty response."
118
 
119
+ return answer
 
 
120
 
121
  # =========================
122
  # Gradio UI
123
  # =========================
124
+ with gr.Blocks(title="RAG Chatbot") as demo:
125
+ gr.Markdown("## πŸ€– RAG Chatbot (TXT-based)")
 
 
 
126
 
127
  chatbot = gr.Chatbot(height=500)
128
 
129
  with gr.Row():
130
+ textbox = gr.Textbox(
131
+ placeholder="Ask about ai-geoth.txt...",
132
  show_label=False
133
  )
134
+ send = gr.Button("Send")
 
 
 
 
 
135
 
136
+ def respond(msg, history):
137
+ reply = smart_answer(msg)
138
+ history.append((msg, reply))
139
+ return "", history
140
 
141
+ textbox.submit(respond, [textbox, chatbot], [textbox, chatbot])
142
+ send.click(respond, [textbox, chatbot], [textbox, chatbot])
 
 
 
143
 
144
  # =========================
145
+ # Startup
146
  # =========================
147
  if __name__ == "__main__":
148
+ text = load_hidden_file()
149
+ build_vector_db(text)
150
  demo.launch()