Reality123b commited on
Commit
449a90d
·
verified ·
1 Parent(s): 7576295

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -50
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import gradio as gr
2
  import os
3
  from huggingface_hub import InferenceClient
4
- import time
5
 
6
  hf_token = os.getenv("hf_token")
7
 
@@ -25,22 +24,12 @@ def get_response(user_input):
25
  response = ""
26
  for chunk in stream:
27
  response += chunk.choices[0].delta.content
28
- yield response # Yielding progressively as the model generates output
29
- time.sleep(0.05) # Optional: Adjust speed of the stream (in seconds)
30
 
31
  def chat_interface():
32
  with gr.Blocks() as demo:
33
- with gr.Row(): # No 'min_width' argument here
34
- with gr.Column(): # No 'min_width' argument here
35
- chat_output = gr.Chatbot(
36
- elem_id="chat-box",
37
- label="Xylaria 1.4 Senoa Chatbot",
38
- show_label=False,
39
- type="messages" # Specify type for correct message format
40
- )
41
-
42
- with gr.Row(elem_id="input-row"):
43
- with gr.Column(): # No 'min_width' argument here
44
  input_textbox = gr.Textbox(
45
  label="Type your message",
46
  placeholder="Ask me anything...",
@@ -50,48 +39,23 @@ def chat_interface():
50
  elem_id="user-input",
51
  show_label=False
52
  )
53
- with gr.Column():
54
  send_button = gr.Button("Send", elem_id="send-btn")
55
 
 
 
 
 
 
 
56
  def submit_input(user_input, chat_history):
57
- chat_history.append({"role": "user", "content": user_input})
58
- return "", chat_history # Clear input field
 
59
 
60
  input_textbox.submit(submit_input, [input_textbox, chat_output], [input_textbox, chat_output])
61
  send_button.click(submit_input, [input_textbox, chat_output], [input_textbox, chat_output])
62
-
63
- def handle_response(user_input, chat_history):
64
- # Initialize chat_history if it's empty
65
- if not chat_history:
66
- chat_history = [{"role": "system", "content": "you are xylaria 1.4 senoa, developed by sk md saad amin"}]
67
-
68
- # Add user input
69
- chat_history.append({"role": "user", "content": user_input})
70
-
71
- # Get response from model
72
- response_stream = get_response(user_input)
73
- for partial_response in response_stream:
74
- chat_history[-1] = {"role": "assistant", "content": partial_response}
75
- yield "", chat_history # Return the updated chat history progressively
76
-
77
- input_textbox.submit(handle_response, [input_textbox, chat_output], [input_textbox, chat_output])
78
- send_button.click(handle_response, [input_textbox, chat_output], [input_textbox, chat_output])
79
-
80
- demo.css = """
81
- #input-row {
82
- position: absolute;
83
- bottom: 10px;
84
- width: 100%;
85
- padding: 10px;
86
- background-color: #f5f5f5;
87
- border-top: 1px solid #ddd;
88
- }
89
- #chat-box {
90
- height: calc(100vh - 100px); /* Adjust the height of chat history */
91
- overflow-y: scroll;
92
- }
93
- """
94
-
95
  return demo
96
 
97
  demo = chat_interface()
 
1
  import gradio as gr
2
  import os
3
  from huggingface_hub import InferenceClient
 
4
 
5
  hf_token = os.getenv("hf_token")
6
 
 
24
  response = ""
25
  for chunk in stream:
26
  response += chunk.choices[0].delta.content
27
+ return response
 
28
 
29
  def chat_interface():
30
  with gr.Blocks() as demo:
31
+ with gr.Row():
32
+ with gr.Column(scale=0.8):
 
 
 
 
 
 
 
 
 
33
  input_textbox = gr.Textbox(
34
  label="Type your message",
35
  placeholder="Ask me anything...",
 
39
  elem_id="user-input",
40
  show_label=False
41
  )
42
+ with gr.Column(scale=0.2):
43
  send_button = gr.Button("Send", elem_id="send-btn")
44
 
45
+ chat_output = gr.Chatbot(
46
+ elem_id="chat-box",
47
+ label="Xylaria 1.4 Senoa Chatbot",
48
+ show_label=False
49
+ )
50
+
51
  def submit_input(user_input, chat_history):
52
+ response = get_response(user_input)
53
+ chat_history.append((user_input, response))
54
+ return "", chat_history
55
 
56
  input_textbox.submit(submit_input, [input_textbox, chat_output], [input_textbox, chat_output])
57
  send_button.click(submit_input, [input_textbox, chat_output], [input_textbox, chat_output])
58
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  return demo
60
 
61
  demo = chat_interface()