Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
# بررسی بازخورد کارفرما درباره متن شغلی
|
272 |
-
if job_description and not job_description_confirmed:
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
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
|