dnzblgn commited on
Commit
fd10698
·
verified ·
1 Parent(s): cf2010c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -61
app.py CHANGED
@@ -178,24 +178,15 @@ def demo():
178
  .chat-message.user .text { align-self: flex-end; background: #d1e7dd; }
179
  .chat-message.assistant .text { align-self: flex-start; background: #e2e3e5; }
180
  .gradio-container { max-width: 1000px !important; margin: auto; font-family: 'Segoe UI', sans-serif; }
181
- .gr-button { font-weight: bold; background-color: #004085 !important; color: white; }
182
  """) as app:
183
 
184
  db_state = gr.State(None)
185
  chain_state = gr.State(None)
186
- model_selection = gr.Dropdown(
187
- choices=[
188
- "mistralai/Mistral-7B-Instruct-v0.3",
189
- "HuggingFaceH4/zephyr-7b-beta",
190
- "tiiuae/falcon-7b-instruct"
191
- ],
192
- value="mistralai/Mistral-7B-Instruct-v0.3",
193
- label="Choose Generative Model"
194
- )
195
 
196
  gr.Markdown("""
197
  # 🧠 Customer Review Analyzer
198
- Chat with your reviews using **custom LLMs**. Upload a `.txt` file of reviews and get insights instantly.
199
  """)
200
 
201
  with gr.Row():
@@ -209,58 +200,11 @@ def demo():
209
  user_input = gr.Textbox(placeholder="Ask about your reviews...", show_label=False)
210
  submit_btn = gr.Button("Send", variant="secondary")
211
 
212
- # Hook updated LLM init with selected model
213
- def process_and_initialize_with_model(file, selected_model):
214
- token = get_hf_token()
215
- if file is None or not token:
216
- return None, None, "Please upload a file and ensure HF token is set."
217
- with open(file, 'r', encoding='utf-8') as f:
218
- reviews = [line.strip() for line in f if line.strip()]
219
- analysis = analyze_reviews(reviews)
220
- doc = generate_analysis_document(analysis)
221
- db = create_db_from_analysis(doc)
222
- rag_chain = initialize_rag_chatbot_with_model(db, selected_model)
223
- return db, rag_chain, f"Processed {len(reviews)} reviews with {selected_model}. Ready to chat!"
224
-
225
- def initialize_rag_chatbot_with_model(db, selected_model):
226
- token = get_hf_token()
227
- if not token:
228
- return None
229
- memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True, output_key="answer")
230
- retriever = db.as_retriever(search_kwargs={"k": 4})
231
- llm = FallbackLLM(token=token, model_list=[selected_model])
232
- return ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever, memory=memory, return_source_documents=True)
233
-
234
- # Update interaction to show only messages
235
- def chat_display_only(query, qa_chain, chatbot):
236
- history = chatbot or []
237
- if not query.strip():
238
- yield history, ""
239
- return
240
- if qa_chain is None:
241
- history.append((None, "Please upload and process a file first."))
242
- yield history, ""
243
- return
244
- history.append((query, ""))
245
- yield history, ""
246
- try:
247
- response = qa_chain.invoke({"question": query, "chat_history": []})
248
- assistant_response = response.get("answer", "No answer generated.")
249
- for i in range(len(assistant_response)):
250
- history[-1] = (query, assistant_response[:i+1])
251
- yield history, ""
252
- time.sleep(0.01)
253
- except Exception as e:
254
- logger.error(f"RAG error: {e}")
255
- history[-1] = (query, "An error occurred while answering your question.")
256
- yield history, ""
257
-
258
- process_btn.click(process_and_initialize_with_model, inputs=[file_input, model_selection], outputs=[db_state, chain_state, status])
259
- submit_btn.click(chat_display_only, inputs=[user_input, chain_state, chatbot], outputs=[chatbot, user_input])
260
- user_input.submit(chat_display_only, inputs=[user_input, chain_state, chatbot], outputs=[chatbot, user_input])
261
 
262
  return app
263
 
264
-
265
  if __name__ == "__main__":
266
  demo().launch(server_name="0.0.0.0", server_port=7860, share=False)
 
178
  .chat-message.user .text { align-self: flex-end; background: #d1e7dd; }
179
  .chat-message.assistant .text { align-self: flex-start; background: #e2e3e5; }
180
  .gradio-container { max-width: 1000px !important; margin: auto; font-family: 'Segoe UI', sans-serif; }
181
+ .gr-button { font-weight: bold; background-color: #0d6efd !important; color: white; }
182
  """) as app:
183
 
184
  db_state = gr.State(None)
185
  chain_state = gr.State(None)
 
 
 
 
 
 
 
 
 
186
 
187
  gr.Markdown("""
188
  # 🧠 Customer Review Analyzer
189
+ Upload a `.txt` file of reviews and chat with a custom AI system based on your data.
190
  """)
191
 
192
  with gr.Row():
 
200
  user_input = gr.Textbox(placeholder="Ask about your reviews...", show_label=False)
201
  submit_btn = gr.Button("Send", variant="secondary")
202
 
203
+ process_btn.click(process_and_initialize, inputs=[file_input], outputs=[db_state, chain_state, status])
204
+ submit_btn.click(user_query_with_rag, inputs=[user_input, chain_state, chatbot], outputs=[chatbot, user_input])
205
+ user_input.submit(user_query_with_rag, inputs=[user_input, chain_state, chatbot], outputs=[chatbot, user_input])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
 
207
  return app
208
 
 
209
  if __name__ == "__main__":
210
  demo().launch(server_name="0.0.0.0", server_port=7860, share=False)