Baobab4983 commited on
Commit
8ce80d5
·
1 Parent(s): 4e95c16

My initial chatbot

Browse files
Files changed (1) hide show
  1. app.py +41 -37
app.py CHANGED
@@ -64,14 +64,12 @@ css = """
64
  }
65
  """
66
 
67
- # initialize the chat model and sets up the Gradio interface
68
  def registry(name, token, examples=None, **kwargs):
69
  client = genai.Client(api_key=token)
70
  chat_locks = {} # Dictionary to hold locks for each user's chat
71
  chat_sessions = {} # Dictionary to hold each user chat
72
-
73
 
74
- # create a new chat session with the specified model and configuration
75
  def create_chat():
76
  return client.chats.create(
77
  model=name,
@@ -81,47 +79,53 @@ def registry(name, token, examples=None, **kwargs):
81
  ),
82
  )
83
 
84
-
85
  # send a user message to Gemini, streams the response back to the chatbot
86
  # and updates the history
87
  def stream_response(history, text, img, request: gr.Request):
88
- user_id = request.client.host
89
- if user_id not in chat_locks:
90
- chat_locks[user_id] = Lock()
91
- chat_sessions[user_id] = create_chat()
92
-
93
- lock = chat_locks[user_id]
94
- chat = chat_sessions[user_id]
95
-
96
- try:
97
- with lock:
98
- if not img:
99
- response_stream = chat.send_message_stream(text)
100
- else:
101
- try:
102
- img = Image.open(img)
103
- response_stream = chat.send_message_stream([text, img])
104
- except Exception as e:
105
- print(f"Error processing image: {str(e)}")
106
- return
107
-
108
- partial_message = ""
109
- for chunk in response_stream:
110
- if chunk.text:
111
- partial_message += chunk.text
112
- history[-1] = (history[-1][0], partial_message)
113
- yield history
114
- time.sleep(0.02) # Small delay to ensure smooth streaming
115
-
116
- except Exception as e:
117
- print(f"Error in stream_response: {str(e)}")
118
- return
 
 
 
 
 
 
 
119
 
120
  print("Building the gradio app...")
121
  with gr.Blocks(css=css) as app:
122
  gr.HTML(TITLE)
123
  gr.HTML(SUBTITLE)
124
-
125
  with gr.Row():
126
  image_box = gr.Image(type="filepath")
127
  chatbot = gr.Chatbot(
@@ -136,7 +140,7 @@ def registry(name, token, examples=None, **kwargs):
136
  )
137
 
138
  btn = gr.Button("Send")
139
-
140
  # Update the event handlers to use streaming
141
  btn.click(
142
  fn=query_message,
 
64
  }
65
  """
66
 
67
+ # gradio chatbot main function
68
  def registry(name, token, examples=None, **kwargs):
69
  client = genai.Client(api_key=token)
70
  chat_locks = {} # Dictionary to hold locks for each user's chat
71
  chat_sessions = {} # Dictionary to hold each user chat
 
72
 
 
73
  def create_chat():
74
  return client.chats.create(
75
  model=name,
 
79
  ),
80
  )
81
 
 
82
  # send a user message to Gemini, streams the response back to the chatbot
83
  # and updates the history
84
  def stream_response(history, text, img, request: gr.Request):
85
+ user_id = request.client.host
86
+ if user_id not in chat_locks:
87
+ chat_locks[user_id] = Lock()
88
+ chat_sessions[user_id] = create_chat()
89
+
90
+ lock = chat_locks[user_id]
91
+ chat = chat_sessions[user_id]
92
+
93
+ try:
94
+ with lock:
95
+ if not img:
96
+ response_stream = chat.send_message_stream(
97
+ text
98
+ )
99
+ else:
100
+ try:
101
+ img = Image.open(img)
102
+ response_stream = chat.send_message_stream(
103
+ [text, img]
104
+ )
105
+ except Exception as e:
106
+ print(f"Error processing image: {str(e)}")
107
+ return
108
+
109
+ # Initialize response text
110
+ response_text = ""
111
+
112
+ # Stream the response
113
+ for chunk in response_stream:
114
+ if chunk.text:
115
+ response_text += chunk.text
116
+ # Update the last message in history with the new content
117
+ history[-1] = (history[-1][0], response_text)
118
+ yield history
119
+
120
+ except Exception as e:
121
+ print(f"Error in stream_response: {str(e)}")
122
+ return
123
 
124
  print("Building the gradio app...")
125
  with gr.Blocks(css=css) as app:
126
  gr.HTML(TITLE)
127
  gr.HTML(SUBTITLE)
128
+
129
  with gr.Row():
130
  image_box = gr.Image(type="filepath")
131
  chatbot = gr.Chatbot(
 
140
  )
141
 
142
  btn = gr.Button("Send")
143
+
144
  # Update the event handlers to use streaming
145
  btn.click(
146
  fn=query_message,