hema05core commited on
Commit
46bc2ba
·
verified ·
1 Parent(s): dd5caaa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -19
app.py CHANGED
@@ -29,7 +29,7 @@ retriever = db.as_retriever(search_kwargs={"k": 3})
29
  # --- Hugging Face Hub LLM setup with secret token ---
30
  llm = HuggingFaceHub(
31
  repo_id="google/flan-t5-base", # smaller for faster response
32
- model_kwargs={"temperature":0},
33
  huggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN")
34
  )
35
 
@@ -40,14 +40,15 @@ qa = ConversationalRetrievalChain.from_llm(
40
 
41
  chat_history = []
42
 
43
- def respond(message, history):
 
44
  global chat_history
45
  chat_history = chat_history[-6:] # keep last 3 exchanges
46
  result = qa({"question": message, "chat_history": chat_history})
47
  chat_history.append((message, result["answer"]))
48
- return result["answer"], chat_history
49
 
50
- # --- 5️⃣ Gradio Blocks UI with Entry Warning ---
51
  with gr.Blocks() as demo:
52
  with gr.Column():
53
  # Warning message
@@ -72,24 +73,45 @@ with gr.Blocks() as demo:
72
  user_input = gr.Textbox(placeholder="Type your message here...", visible=False)
73
  submit_btn = gr.Button("Send", visible=False)
74
 
75
- # Button interactions
76
  def enter_case():
77
- chatbot.visible = True
78
- user_input.visible = True
79
- submit_btn.visible = True
80
- warning_text.update(value="") # hide warning
81
- enter_btn.visible = False
82
- exit_btn.visible = False
83
- return gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
 
84
 
85
  def exit_case():
86
- return gr.update(value="<h2>Session ended. You exited the simulation.</h2>"), None, None
87
-
88
- enter_btn.click(enter_case)
89
- exit_btn.click(exit_case)
90
-
91
- submit_btn.click(respond, inputs=[user_input, chatbot], outputs=[chatbot, chatbot])
 
 
92
 
93
- # --- 6️⃣ Launch ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  if __name__ == "__main__":
95
  demo.launch(share=True, enable_queue=True)
 
29
  # --- Hugging Face Hub LLM setup with secret token ---
30
  llm = HuggingFaceHub(
31
  repo_id="google/flan-t5-base", # smaller for faster response
32
+ model_kwargs={"temperature": 0},
33
  huggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN")
34
  )
35
 
 
40
 
41
  chat_history = []
42
 
43
+ # --- 5️⃣ Respond function ---
44
+ def respond(message, chat_history):
45
  global chat_history
46
  chat_history = chat_history[-6:] # keep last 3 exchanges
47
  result = qa({"question": message, "chat_history": chat_history})
48
  chat_history.append((message, result["answer"]))
49
+ return chat_history, chat_history
50
 
51
+ # --- 6️⃣ Gradio UI with Entry Warning ---
52
  with gr.Blocks() as demo:
53
  with gr.Column():
54
  # Warning message
 
73
  user_input = gr.Textbox(placeholder="Type your message here...", visible=False)
74
  submit_btn = gr.Button("Send", visible=False)
75
 
76
+ # --- Button interactions ---
77
  def enter_case():
78
+ return (
79
+ gr.update(visible=True), # chatbot
80
+ gr.update(visible=True), # user_input
81
+ gr.update(visible=True), # submit_btn
82
+ gr.update(value=""), # hide warning
83
+ gr.update(visible=False), # hide enter_btn
84
+ gr.update(visible=False) # hide exit_btn
85
+ )
86
 
87
  def exit_case():
88
+ return (
89
+ gr.update(value="<h2>Session ended. You exited the simulation.</h2>"), # hide warning text
90
+ gr.update(visible=False), # chatbot
91
+ gr.update(visible=False), # user_input
92
+ gr.update(visible=False), # submit_btn
93
+ gr.update(visible=False), # enter_btn
94
+ gr.update(visible=False) # exit_btn
95
+ )
96
 
97
+ enter_btn.click(
98
+ enter_case,
99
+ inputs=None,
100
+ outputs=[chatbot, user_input, submit_btn, warning_text, enter_btn, exit_btn]
101
+ )
102
+
103
+ exit_btn.click(
104
+ exit_case,
105
+ inputs=None,
106
+ outputs=[warning_text, chatbot, user_input, submit_btn, enter_btn, exit_btn]
107
+ )
108
+
109
+ submit_btn.click(
110
+ respond,
111
+ inputs=[user_input, chatbot],
112
+ outputs=[chatbot, chatbot]
113
+ )
114
+
115
+ # --- 7️⃣ Launch ---
116
  if __name__ == "__main__":
117
  demo.launch(share=True, enable_queue=True)