Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -104,12 +104,12 @@ def encode_local_image(image):
|
|
104 |
pil_image.save(buffer, format="JPEG")
|
105 |
return f"data:image/jpeg;base64,{base64.b64encode(buffer.getvalue()).decode('utf-8')}"
|
106 |
|
107 |
-
# Describe image using the model
|
108 |
-
def describe_image(image_path):
|
109 |
image_string = encode_local_image(image_path)
|
110 |
|
111 |
messages = [
|
112 |
-
{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": os.getenv('USER_PROMPT')}]}
|
113 |
]
|
114 |
|
115 |
prompt_with_template = idefics_processor.apply_chat_template(
|
@@ -123,12 +123,24 @@ def describe_image(image_path):
|
|
123 |
"parameters": {"return_full_text": False, "max_new_tokens": 2048},
|
124 |
}
|
125 |
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
# Telegram bot client
|
130 |
client = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)
|
131 |
|
|
|
|
|
|
|
|
|
|
|
132 |
# Async function to get OpenAI completion
|
133 |
async def get_completion(event, user_id, prompt):
|
134 |
async with client.action(event.chat_id, 'typing'):
|
@@ -144,14 +156,14 @@ async def get_completion(event, user_id, prompt):
|
|
144 |
model=model,
|
145 |
messages=messages,
|
146 |
max_tokens=512,
|
147 |
-
temperature=0.
|
148 |
top_p=1.0,
|
149 |
frequency_penalty=0.9,
|
150 |
presence_penalty=0.9,
|
151 |
)
|
152 |
message = response.choices[0].message.content
|
153 |
except Exception as e:
|
154 |
-
message = f"
|
155 |
print(e)
|
156 |
|
157 |
update_chat_history(user_id, "user", prompt) # Update history
|
@@ -161,11 +173,11 @@ async def get_completion(event, user_id, prompt):
|
|
161 |
# Telegram bot events
|
162 |
@client.on(events.NewMessage(pattern='/start'))
|
163 |
async def start(event):
|
164 |
-
await event.respond("Hello!
|
165 |
|
166 |
@client.on(events.NewMessage(pattern='/help'))
|
167 |
async def help(event):
|
168 |
-
await event.respond("Here is how I can help you:\n/start -
|
169 |
|
170 |
@client.on(events.NewMessage(pattern='/reset'))
|
171 |
async def reset(event):
|
@@ -177,16 +189,24 @@ async def reset(event):
|
|
177 |
|
178 |
@client.on(events.NewMessage)
|
179 |
async def handle_message(event):
|
|
|
|
|
180 |
try:
|
181 |
user_id = event.chat_id # Use chat_id to distinguish between users
|
182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
if event.photo:
|
|
|
184 |
photo = await event.download_media()
|
185 |
-
image_description = describe_image(photo)
|
186 |
-
user_message
|
187 |
-
else:
|
188 |
-
user_message = event.raw_text
|
189 |
|
|
|
190 |
if user_message.startswith('/start') or user_message.startswith('/help') or user_message.startswith('/reset'):
|
191 |
return
|
192 |
|
@@ -194,7 +214,7 @@ async def handle_message(event):
|
|
194 |
await event.respond(response)
|
195 |
except Exception as e:
|
196 |
print(f"An error occurred: {e}")
|
197 |
-
await event.respond("Whoopsie
|
198 |
|
199 |
# Keep-alive function to keep the bot running
|
200 |
def keep_alive():
|
@@ -209,10 +229,8 @@ def keep_alive():
|
|
209 |
model=model,
|
210 |
messages=messages,
|
211 |
max_tokens=10,
|
212 |
-
temperature=0.
|
213 |
-
top_p=0.
|
214 |
-
frequency_penalty=0.2,
|
215 |
-
presence_penalty=0.6,
|
216 |
)
|
217 |
print(request)
|
218 |
except Exception as e:
|
|
|
104 |
pil_image.save(buffer, format="JPEG")
|
105 |
return f"data:image/jpeg;base64,{base64.b64encode(buffer.getvalue()).decode('utf-8')}"
|
106 |
|
107 |
+
# Describe image using the model with error handling
|
108 |
+
def describe_image(image_path, query=''):
|
109 |
image_string = encode_local_image(image_path)
|
110 |
|
111 |
messages = [
|
112 |
+
{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": f"{os.getenv('USER_PROMPT')}\n{query}" }]}
|
113 |
]
|
114 |
|
115 |
prompt_with_template = idefics_processor.apply_chat_template(
|
|
|
123 |
"parameters": {"return_full_text": False, "max_new_tokens": 2048},
|
124 |
}
|
125 |
|
126 |
+
try:
|
127 |
+
response = idefics_client.post(json=payload)
|
128 |
+
# Check if the response is empty or not valid JSON
|
129 |
+
if response.status_code != 200 or not response.text:
|
130 |
+
raise ValueError(f"Invalid response: {response.status_code}, {response.text}")
|
131 |
+
return response.text
|
132 |
+
except Exception as e:
|
133 |
+
print(f"Error during image description: {e}")
|
134 |
+
return "Unable to describe the image due to an error."
|
135 |
|
136 |
# Telegram bot client
|
137 |
client = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)
|
138 |
|
139 |
+
# Get bot user ID for message filtering
|
140 |
+
async def get_bot_id():
|
141 |
+
me = await client.get_me()
|
142 |
+
return me.id
|
143 |
+
|
144 |
# Async function to get OpenAI completion
|
145 |
async def get_completion(event, user_id, prompt):
|
146 |
async with client.action(event.chat_id, 'typing'):
|
|
|
156 |
model=model,
|
157 |
messages=messages,
|
158 |
max_tokens=512,
|
159 |
+
temperature=0.6,
|
160 |
top_p=1.0,
|
161 |
frequency_penalty=0.9,
|
162 |
presence_penalty=0.9,
|
163 |
)
|
164 |
message = response.choices[0].message.content
|
165 |
except Exception as e:
|
166 |
+
message = f"Whoops!"
|
167 |
print(e)
|
168 |
|
169 |
update_chat_history(user_id, "user", prompt) # Update history
|
|
|
173 |
# Telegram bot events
|
174 |
@client.on(events.NewMessage(pattern='/start'))
|
175 |
async def start(event):
|
176 |
+
await event.respond("Hello!")
|
177 |
|
178 |
@client.on(events.NewMessage(pattern='/help'))
|
179 |
async def help(event):
|
180 |
+
await event.respond("Here is how I can help you:\n/start - To check if I am alive\n/help - Show this message\n/reset - Reset chat history")
|
181 |
|
182 |
@client.on(events.NewMessage(pattern='/reset'))
|
183 |
async def reset(event):
|
|
|
189 |
|
190 |
@client.on(events.NewMessage)
|
191 |
async def handle_message(event):
|
192 |
+
bot_id = await get_bot_id() # Get bot ID to avoid responding to itself
|
193 |
+
|
194 |
try:
|
195 |
user_id = event.chat_id # Use chat_id to distinguish between users
|
196 |
|
197 |
+
# Ignore messages from the bot itself
|
198 |
+
if event.sender_id == bot_id:
|
199 |
+
return
|
200 |
+
|
201 |
+
user_message = event.raw_text
|
202 |
+
|
203 |
if event.photo:
|
204 |
+
# If an image is sent, describe the image
|
205 |
photo = await event.download_media()
|
206 |
+
image_description = describe_image(photo, user_message)
|
207 |
+
user_message += f"\n\nContent of the image: {image_description}"
|
|
|
|
|
208 |
|
209 |
+
# Ignore command messages to prevent double processing
|
210 |
if user_message.startswith('/start') or user_message.startswith('/help') or user_message.startswith('/reset'):
|
211 |
return
|
212 |
|
|
|
214 |
await event.respond(response)
|
215 |
except Exception as e:
|
216 |
print(f"An error occurred: {e}")
|
217 |
+
await event.respond("Whoopsie!")
|
218 |
|
219 |
# Keep-alive function to keep the bot running
|
220 |
def keep_alive():
|
|
|
229 |
model=model,
|
230 |
messages=messages,
|
231 |
max_tokens=10,
|
232 |
+
temperature=0.1,
|
233 |
+
top_p=0.1,
|
|
|
|
|
234 |
)
|
235 |
print(request)
|
236 |
except Exception as e:
|