rbn2008k commited on
Commit
cded38f
·
verified ·
1 Parent(s): b789864

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -20
app.py CHANGED
@@ -21,6 +21,7 @@ def load_system_prompt():
21
 
22
  system_prompt = load_system_prompt()
23
 
 
24
  api_id = os.getenv('api_id')
25
  api_hash = os.getenv('api_hash')
26
  bot_token = os.getenv('bot_token')
@@ -28,10 +29,10 @@ openai_api_key = os.getenv('glhf')
28
  ping_key = os.getenv('bolo')
29
  api_url = os.getenv('yolo')
30
  model = os.getenv('model')
31
- model1 = os.getenv('model1')
32
  model2 = os.getenv('model2')
33
  mongoURI = os.getenv('MONGO_URI')
34
 
 
35
  openai_client = OpenAI(api_key=openai_api_key, base_url=api_url)
36
  mongo_client = MongoClient(mongoURI)
37
  db = mongo_client['Scarlett']
@@ -40,6 +41,7 @@ chat_collection = db['chats']
40
  local_chat_history = OrderedDict()
41
  MAX_LOCAL_USERS = 5
42
 
 
43
  def get_history_from_mongo(user_id):
44
  result = chat_collection.find_one({"user_id": user_id})
45
  return result.get("messages", []) if result else []
@@ -77,28 +79,45 @@ def update_chat_history(user_id, role, content):
77
  local_chat_history.popitem(last=False)
78
  store_message_in_mongo(user_id, role, content)
79
 
80
- def encode_local_image(image_path):
81
- im = Image.open(image_path)
82
- buffered = BytesIO()
83
- im.save(buffered, format="PNG")
84
- image_bytes = buffered.getvalue()
85
- image_base64 = base64.b64encode(image_bytes).decode('ascii')
86
- return image_base64
 
 
 
 
 
87
 
 
88
  def inference_calling_idefics(image_path, question=""):
89
  system_prompt = os.getenv('USER_PROMPT')
90
  model_id = model2
91
  client = InferenceClient(model=model_id)
92
- image_base64 = describe_image(image_path)
 
 
 
 
 
 
93
  image_info = f"data:image/png;base64,{image_base64}"
94
  prompt = f"{system_prompt}\n![]({image_info})\n{question}\n\n"
95
- response = client.text_generation(
96
- prompt,
97
- max_new_tokens=512,
98
- do_sample=True,
99
- temperature=0.2
100
- )
101
- return response
 
 
 
 
 
102
 
103
  def describe_image(image_path, question=""):
104
  try:
@@ -108,12 +127,14 @@ def describe_image(image_path, question=""):
108
  print(e)
109
  return "Error while seeing the image."
110
 
 
111
  client = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)
112
 
113
  async def get_bot_id():
114
  me = await client.get_me()
115
  return me.id
116
 
 
117
  async def get_completion(event, user_id, prompt):
118
  async with client.action(event.chat_id, 'typing'):
119
  await asyncio.sleep(3)
@@ -139,12 +160,13 @@ async def get_completion(event, user_id, prompt):
139
  if chunk.choices[0].delta.content is not None:
140
  message += chunk.choices[0].delta.content
141
  except Exception as e:
142
- message = f"Whoops!"
143
  print(e)
144
  update_chat_history(user_id, "user", prompt)
145
  update_chat_history(user_id, "assistant", message)
146
  return message
147
 
 
148
  @client.on(events.NewMessage(pattern='/start'))
149
  async def start(event):
150
  await event.respond("Hello!")
@@ -173,8 +195,9 @@ async def handle_message(event):
173
  user_message = event.raw_text
174
  if event.photo:
175
  photo = await event.download_media()
176
- image_description = describe_image(photo, user_message)
177
- user_message += f"\n\nI sent you an image. Content of the image: {image_description}"
 
178
  if user_message.startswith('/start') or user_message.startswith('/help') or user_message.startswith('/reset'):
179
  return
180
  response = await get_completion(event, user_id, user_message)
@@ -183,6 +206,7 @@ async def handle_message(event):
183
  print(f"An error occurred: {e}")
184
  await event.respond("Whoopsie!")
185
 
 
186
  def launch_gradio():
187
  welcome_message = """
188
  # Meet Scarlett!
@@ -197,12 +221,13 @@ def launch_gradio():
197
  """)
198
  demo.launch(show_api=False)
199
 
 
200
  def keep_alive():
201
  ping_client = OpenAI(api_key=ping_key, base_url=api_url)
202
  while True:
203
  try:
204
  messages = [
205
- {"role": "system", "content": "Repeat what i say."},
206
  {"role": "user", "content": "Repeat: 'Ping success'"}
207
  ]
208
  request = ping_client.chat.completions.create(
@@ -217,6 +242,7 @@ def keep_alive():
217
  print(f"Keep-alive request failed: {e}")
218
  time.sleep(1800)
219
 
 
220
  if __name__ == "__main__":
221
  threading.Thread(target=keep_alive).start()
222
  threading.Thread(target=launch_gradio).start()
 
21
 
22
  system_prompt = load_system_prompt()
23
 
24
+ # Environment variables
25
  api_id = os.getenv('api_id')
26
  api_hash = os.getenv('api_hash')
27
  bot_token = os.getenv('bot_token')
 
29
  ping_key = os.getenv('bolo')
30
  api_url = os.getenv('yolo')
31
  model = os.getenv('model')
 
32
  model2 = os.getenv('model2')
33
  mongoURI = os.getenv('MONGO_URI')
34
 
35
+ # OpenAI and MongoDB clients
36
  openai_client = OpenAI(api_key=openai_api_key, base_url=api_url)
37
  mongo_client = MongoClient(mongoURI)
38
  db = mongo_client['Scarlett']
 
41
  local_chat_history = OrderedDict()
42
  MAX_LOCAL_USERS = 5
43
 
44
+ # Functions for MongoDB-based chat history storage and retrieval
45
  def get_history_from_mongo(user_id):
46
  result = chat_collection.find_one({"user_id": user_id})
47
  return result.get("messages", []) if result else []
 
79
  local_chat_history.popitem(last=False)
80
  store_message_in_mongo(user_id, role, content)
81
 
82
+ # Fixing image encoding
83
+ def encode_local_image(image_file):
84
+ try:
85
+ im = Image.open(image_file)
86
+ buffered = BytesIO()
87
+ im.save(buffered, format="PNG")
88
+ image_bytes = buffered.getvalue()
89
+ image_base64 = base64.b64encode(image_bytes).decode('ascii')
90
+ return image_base64
91
+ except Exception as e:
92
+ print(f"Error encoding image: {e}")
93
+ return None
94
 
95
+ # Image description function, calling external inference model
96
  def inference_calling_idefics(image_path, question=""):
97
  system_prompt = os.getenv('USER_PROMPT')
98
  model_id = model2
99
  client = InferenceClient(model=model_id)
100
+
101
+ # Use the fixed `encode_local_image` to encode the image
102
+ image_base64 = encode_local_image(image_path)
103
+
104
+ if not image_base64:
105
+ return "Error: Invalid image or unable to encode image."
106
+
107
  image_info = f"data:image/png;base64,{image_base64}"
108
  prompt = f"{system_prompt}\n![]({image_info})\n{question}\n\n"
109
+
110
+ try:
111
+ response = client.text_generation(
112
+ prompt,
113
+ max_new_tokens=512,
114
+ do_sample=True,
115
+ temperature=0.2
116
+ )
117
+ return response
118
+ except Exception as e:
119
+ print(f"Error in inference call: {e}")
120
+ return "Error while processing the image."
121
 
122
  def describe_image(image_path, question=""):
123
  try:
 
127
  print(e)
128
  return "Error while seeing the image."
129
 
130
+ # Telegram bot setup
131
  client = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)
132
 
133
  async def get_bot_id():
134
  me = await client.get_me()
135
  return me.id
136
 
137
+ # OpenAI completion handler
138
  async def get_completion(event, user_id, prompt):
139
  async with client.action(event.chat_id, 'typing'):
140
  await asyncio.sleep(3)
 
160
  if chunk.choices[0].delta.content is not None:
161
  message += chunk.choices[0].delta.content
162
  except Exception as e:
163
+ message = "Whoops!"
164
  print(e)
165
  update_chat_history(user_id, "user", prompt)
166
  update_chat_history(user_id, "assistant", message)
167
  return message
168
 
169
+ # Telegram bot commands
170
  @client.on(events.NewMessage(pattern='/start'))
171
  async def start(event):
172
  await event.respond("Hello!")
 
195
  user_message = event.raw_text
196
  if event.photo:
197
  photo = await event.download_media()
198
+ if photo:
199
+ image_description = describe_image(photo, user_message)
200
+ user_message += f"\n\nI sent you an image. Content of the image: {image_description}"
201
  if user_message.startswith('/start') or user_message.startswith('/help') or user_message.startswith('/reset'):
202
  return
203
  response = await get_completion(event, user_id, user_message)
 
206
  print(f"An error occurred: {e}")
207
  await event.respond("Whoopsie!")
208
 
209
+ # Gradio interface
210
  def launch_gradio():
211
  welcome_message = """
212
  # Meet Scarlett!
 
221
  """)
222
  demo.launch(show_api=False)
223
 
224
+ # Keep-alive functionality for the bot
225
  def keep_alive():
226
  ping_client = OpenAI(api_key=ping_key, base_url=api_url)
227
  while True:
228
  try:
229
  messages = [
230
+ {"role": "system", "content": "Repeat what I say."},
231
  {"role": "user", "content": "Repeat: 'Ping success'"}
232
  ]
233
  request = ping_client.chat.completions.create(
 
242
  print(f"Keep-alive request failed: {e}")
243
  time.sleep(1800)
244
 
245
+ # Main execution
246
  if __name__ == "__main__":
247
  threading.Thread(target=keep_alive).start()
248
  threading.Thread(target=launch_gradio).start()