Slaiwala commited on
Commit
e2c805d
·
verified ·
1 Parent(s): 03910cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -55
app.py CHANGED
@@ -1,70 +1,91 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
 
 
 
 
 
 
4
 
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
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
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
20
 
21
- messages.extend(history)
22
 
23
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- response = ""
 
 
 
 
 
26
 
27
- for message in client.chat_completion(
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
- response += token
40
- yield response
41
 
 
 
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- type="messages",
49
- additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
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
+ from askstein import ask
3
 
4
+ def main():
5
+ with gr.Blocks() as demo:
6
+ # Store user info and chat history in state
7
+ user_info = gr.State({"first_name": None, "last_name": None})
8
+ chat_history = gr.State([]) # list of (user_msg, bot_response, feedback)
9
 
10
+ # --- User info input ---
11
+ with gr.Row():
12
+ first_name = gr.Textbox(label="First Name", placeholder="Enter your first name", interactive=True)
13
+ last_name = gr.Textbox(label="Last Name", placeholder="Enter your last name", interactive=True)
14
+ submit_names = gr.Button("Submit")
 
 
 
 
 
 
 
 
15
 
16
+ # --- Chatbot interface (hidden initially) ---
17
+ chatbot = gr.Chatbot(visible=False)
18
+ user_msg = gr.Textbox(placeholder="Ask me anything...", visible=False)
19
+ send_btn = gr.Button("Send", visible=False)
20
+ feedback = gr.Radio(choices=["👍", "👎"], label="Your feedback", visible=False)
21
+ submit_feedback = gr.Button("Submit Feedback", visible=False)
22
+ clear_btn = gr.Button("Clear Chat", visible=False)
23
 
24
+ # --- Functions ---
25
 
26
+ def submit_user_info(fn, ln):
27
+ fn = fn.strip()
28
+ ln = ln.strip()
29
+ if not fn or not ln:
30
+ return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), "Please enter both first and last names."
31
+ user_info.value = {"first_name": fn, "last_name": ln}
32
+ return (
33
+ gr.update(visible=False), # hide first_name
34
+ gr.update(visible=False), # hide last_name
35
+ gr.update(visible=True), # show chatbot
36
+ gr.update(visible=True), # show user_msg
37
+ gr.update(visible=True), # show send_btn
38
+ gr.update(visible=False), # hide feedback (initially)
39
+ gr.update(visible=False), # hide submit_feedback
40
+ gr.update(visible=True), # show clear_btn
41
+ ""
42
+ )
43
 
44
+ def send_message(message, history):
45
+ if not message.strip():
46
+ return history, "", gr.update(visible=False), gr.update(visible=True), gr.update(value=None)
47
+ response = ask(message)
48
+ history = history + [(message, response, None)]
49
+ return history, "", gr.update(visible=True), gr.update(visible=False), gr.update(value=None)
50
 
51
+ def submit_user_feedback(feedback_value, history):
52
+ if not history:
53
+ return history, gr.update(visible=False), "No conversation yet."
54
+ if feedback_value is None:
55
+ return history, gr.update(visible=True), "Please provide feedback."
56
+ # Update last message feedback
57
+ history[-1] = (history[-1][0], history[-1][1], feedback_value)
58
+ return history, gr.update(visible=False), "Thank you for your feedback!"
 
 
 
59
 
60
+ def clear_chat():
61
+ return [], ""
62
 
63
+ # --- Event bindings ---
64
+ submit_names.click(
65
+ submit_user_info,
66
+ inputs=[first_name, last_name],
67
+ outputs=[first_name, last_name, chatbot, user_msg, send_btn, feedback, submit_feedback, clear_btn, gr.Textbox(visible=True, interactive=False, label="Error Message")]
68
+ )
69
 
70
+ send_btn.click(
71
+ send_message,
72
+ inputs=[user_msg, chat_history],
73
+ outputs=[chatbot, user_msg, feedback, submit_feedback, feedback]
74
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
+ submit_feedback.click(
77
+ submit_user_feedback,
78
+ inputs=[feedback, chat_history],
79
+ outputs=[chatbot, feedback, gr.Textbox(visible=True, interactive=False, label="Feedback Status")]
80
+ )
81
 
82
+ clear_btn.click(
83
+ clear_chat,
84
+ outputs=[chatbot, user_msg]
85
+ )
86
+
87
+ demo.launch(server_name="0.0.0.0", server_port=7860)
88
 
89
  if __name__ == "__main__":
90
+ main()
91
+