cdcvd commited on
Commit
5ddcb7c
·
verified ·
1 Parent(s): 408e79b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -72
app.py CHANGED
@@ -225,81 +225,105 @@ def synthesize_speech(text):
225
  except Exception as e:
226
  print("خطا در تبدیل متن به گفتار:", e)
227
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
228
 
 
229
  # تابع اصلی برای مدیریت مکالمه
230
- def agent_respond(message):
231
- global job_description, job_description_confirmed
232
-
233
- # به‌روزرسانی حافظه با پیام کاربر
234
- memory.chat_memory.add_user_message(message)
235
-
236
- # آماده‌سازی پیام‌ها برای LLM
237
- messages = memory.chat_memory.messages.copy()
238
-
239
- # اضافه کردن Prompt به عنوان پیام سیستم
240
- system_prompt = prompt.format(chat_history=get_chat_history_string(memory.chat_memory))
241
- messages.insert(0, SystemMessage(content=system_prompt))
242
-
243
- # تولید پاسخ دستیار
244
- response = llm(messages=messages)
245
-
246
- # افزودن پاسخ دستیار به حافظه
247
- memory.chat_memory.add_ai_message(response.content)
248
-
249
- # بررسی اینکه آیا باید متن شغلی تولید شود
250
- if not job_description_confirmed and "متن شغلی" in response.content.lower():
251
- # تولید متن شغلی بر اساس مکالمه
252
- jd_prompt = f"""
253
- بر اساس مکالمه تا کنون، یک متن شغلی دقیق و حرفه‌ای برای موقعیت مورد نظر بنویسید. وظایف، مسئولیت‌ها، مهارت‌ها و هر جزئیات مرتبط دیگری که کارفرما ارائه داده است را شامل کنید.
254
- مکالمه:
255
- {get_chat_history_string(memory.chat_memory)}
256
- """
257
-
258
- # اضافه کردن Prompt به عنوان پیام سیستم
259
- jd_messages = [
260
- SystemMessage(content=jd_prompt)
261
- ]
262
-
263
- jd_response = llm(messages=jd_messages)
264
- job_description = jd_response.content
265
-
266
- # ارائه متن شغلی به کارفرما
267
- confirmation_message = f"متن شغلی پیشنهادی به شرح زیر است:\n\n{job_description}\n\nلطفاً آن را بررسی کرده و بگویید آیا نیاز به تغییر دارد."
268
- memory.chat_memory.add_ai_message(confirmation_message)
269
- return confirmation_message
270
-
271
- # بررسی بازخورد کارفرما درباره متن شغلی
272
- if job_description and not job_description_confirmed:
273
- if any(word in message.lower() for word in ["تغییر", "ویرایش", "به‌روزرسانی", "اصلاح"]):
274
- # به‌روزرسانی متن شغلی بر اساس بازخورد کارفرما
275
- update_prompt = f"""
276
- کارفرما بازخورد زیر را درباره متن شغلی ارائه داده است:
277
- "{message}"
278
- لطفاً متن شغلی را بر این اساس به‌روزرسانی کنید.
279
- متن شغلی اصلی:
280
- {job_description}
281
- """
282
-
283
- # اضافه کردن Prompt به عنوان پیام سیستم
284
- update_messages = [
285
- SystemMessage(content=update_prompt)
286
- ]
287
-
288
- update_response = llm(messages=update_messages)
289
- job_description = update_response.content
290
-
291
- # ارائه متن شغلی به‌روزشده
292
- updated_message = f"متن شغلی به‌روزرسانی‌شده به شرح زیر است:\n\n{job_description}\n\nآیا این مورد رضایت‌بخش است؟"
293
- memory.chat_memory.add_ai_message(updated_message)
294
- return updated_message
295
- elif any(word in message.lower() for word in ["بله", "موافقم", "رضایت‌بخش است", "خوب است"]):
296
- job_description_confirmed = True
297
- final_message = "عالی! متن شغلی نهایی شد. از زمانی که اختصاص دادید متشکرم."
298
- memory.chat_memory.add_ai_message(final_message)
299
- return final_message
300
-
301
- return response.content
 
 
 
 
 
 
 
 
 
302
 
 
303
  import os
304
  import uuid
305
  from pydub import AudioSegment
 
225
  except Exception as e:
226
  print("خطا در تبدیل متن به گفتار:", e)
227
  return None
228
+ def get_model_response(messages):
229
+ response = client.chat.completions.create(
230
+ model="meta-llama/Llama-3.3-70B-Instruct-Turbo",
231
+ messages=messages,
232
+ max_tokens=1024,
233
+ temperature=0.7,
234
+ top_p=0.7,
235
+ top_k=50,
236
+ repetition_penalty=1,
237
+ stop=["<|eot_id|>", "<|eom_id|>"],
238
+ stream=False,
239
+ )
240
+ return response.choices[0].message["content"]
241
 
242
+
243
  # تابع اصلی برای مدیریت مکالمه
244
+ # def agent_respond(message):
245
+ # global job_description, job_description_confirmed
246
+
247
+ # # به‌روزرسانی حافظه با پیام کاربر
248
+ # memory.chat_memory.add_user_message(message)
249
+
250
+ # # آماده‌سازی پیام‌ها برای LLM
251
+ # messages = memory.chat_memory.messages.copy()
252
+
253
+ # # اضافه کردن Prompt به عنوان پیام سیستم
254
+ # system_prompt = prompt.format(chat_history=get_chat_history_string(memory.chat_memory))
255
+ # messages.insert(0, SystemMessage(content=system_prompt))
256
+
257
+ # # تولید پاسخ دستیار
258
+ # response = llm(messages=messages)
259
+
260
+ # # افزودن پاسخ دستیار به حافظه
261
+ # memory.chat_memory.add_ai_message(response.content)
262
+
263
+ # # بررسی اینکه آیا باید متن شغلی تولید شود
264
+ # if not job_description_confirmed and "متن شغلی" in response.content.lower():
265
+ # # تولید متن شغلی بر اساس مکالمه
266
+ # jd_prompt = f"""
267
+ # بر اساس مکالمه تا کنون، یک متن شغلی دقیق و حرفه‌ای برای موقعیت مورد نظر بنویسید. وظایف، مسئولیت‌ها، مهارت‌ها و هر جزئیات مرتبط دیگری که کارفرما ارائه داده است را شامل کنید.
268
+ # مکالمه:
269
+ # {get_chat_history_string(memory.chat_memory)}
270
+ # """
271
+
272
+ # # اضافه کردن Prompt به عنوان پیام سیستم
273
+ # jd_messages = [
274
+ # SystemMessage(content=jd_prompt)
275
+ # # ]
276
+
277
+ # jd_response = llm(messages=jd_messages)
278
+ # job_description = jd_response.content
279
+
280
+ # # ارائه متن شغلی به کارفرما
281
+ # confirmation_message = f"متن شغلی پیشنهادی به شرح زیر است:\n\n{job_description}\n\nلطفاً آن را بررسی کرده و بگویید آیا نیاز به تغییر دارد."
282
+ # memory.chat_memory.add_ai_message(confirmation_message)
283
+ # return confirmation_message
284
+
285
+ # # بررسی بازخورد کارفرما درباره متن شغلی
286
+ # if job_description and not job_description_confirmed:
287
+ # if any(word in message.lower() for word in ["تغییر", "ویرایش", "به‌روزرسانی", "اصلاح"]):
288
+ # # به‌روزرسانی متن شغلی بر اساس بازخورد کارفرما
289
+ # update_prompt = f"""
290
+ # کارفرما بازخورد زیر را درباره متن شغلی ارائه داده است:
291
+ # "{message}"
292
+ # لطفاً متن شغلی را بر این اساس به‌روزرسانی کنید.
293
+ # متن شغلی اصلی:
294
+ # {job_description}
295
+ # """
296
+
297
+ # # اضافه کردن Prompt به عنوان پیام سیستم
298
+ # update_messages = [
299
+ # SystemMessage(content=update_prompt)
300
+ # ]
301
+
302
+ # update_response = llm(messages=update_messages)
303
+ # job_description = update_response.content
304
+
305
+ # # ارائه متن شغلی به‌روزشده
306
+ # updated_message = f"متن شغلی به‌روزرسانی‌شده به شرح زیر است:\n\n{job_description}\n\nآیا این مورد رضایت‌بخش است؟"
307
+ # memory.chat_memory.add_ai_message(updated_message)
308
+ # return updated_message
309
+ # elif any(word in message.lower() for word in ["بله", "موافقم", "رضایت‌بخش است", "خوب است"]):
310
+ # job_description_confirmed = True
311
+ # final_message = "عالی! متن شغلی نهایی شد. از زمانی که اختصاص دادید متشکرم."
312
+ # memory.chat_memory.add_ai_message(final_message)
313
+ # return final_message
314
+
315
+ # return response.content
316
+
317
+
318
+
319
+ def agent_respond(message, chat_history):
320
+ messages = [{"role": "user", "content": message}]
321
+ bot_response = get_model_response(messages)
322
+ chat_history.append((message, bot_response))
323
+ audio_response = synthesize_speech(bot_response)
324
+ return chat_history, "", audio_response
325
 
326
+
327
  import os
328
  import uuid
329
  from pydub import AudioSegment