Spaces:
Sleeping
Sleeping
Commit
·
8ce80d5
1
Parent(s):
4e95c16
My initial chatbot
Browse files
app.py
CHANGED
@@ -64,14 +64,12 @@ css = """
|
|
64 |
}
|
65 |
"""
|
66 |
|
67 |
-
#
|
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 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|