Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -19,7 +19,7 @@ faq_output = ""
|
|
19 |
business_info = None
|
20 |
product_info = None
|
21 |
last_user_request = ""
|
22 |
-
initial_prompt = ""
|
23 |
|
24 |
# پرامپت مادر (بدون تغییر)
|
25 |
mother_prompt = """
|
@@ -84,9 +84,9 @@ def start_process(file1, file2, user_request, user_prompt):
|
|
84 |
iteration_history += f"**درخواست کاربر (فقط برای این Iteration):** {last_user_request}\n"
|
85 |
if initial_prompt:
|
86 |
iteration_history += f"**پرامپت اولیه کاربر:**\n{initial_prompt}\n"
|
87 |
-
prompt_output = initial_prompt
|
88 |
|
89 |
-
dce_instructions = f"iteration {iteration_count}: اگه پرامپت اولیه کاربر وجود داره، از اون بهعنوان پایه استفاده کن. اگه درخواست کاربر
|
90 |
iteration_history += f"**دستورات DCE:** {dce_instructions}\n"
|
91 |
|
92 |
pee_prompt = f"""
|
@@ -95,9 +95,9 @@ def start_process(file1, file2, user_request, user_prompt):
|
|
95 |
اطلاعات کسبوکار: {json.dumps(business_info_serializable, ensure_ascii=False)}
|
96 |
اطلاعات محصولات: {json.dumps(product_info_serializable, ensure_ascii=False)}
|
97 |
پرامپت اولیه کاربر: {initial_prompt if initial_prompt else "هیچ پرامپتی وارد نشده"}
|
98 |
-
درخواست
|
99 |
{dce_instructions}
|
100 |
-
خروجی رو با --- جدا
|
101 |
"""
|
102 |
pee_response = client.chat.completions.create(
|
103 |
model="gpt-4o",
|
@@ -106,9 +106,14 @@ def start_process(file1, file2, user_request, user_prompt):
|
|
106 |
pee_output = pee_response.choices[0].message.content
|
107 |
iteration_history += f"**خروجی PEE:**\n{pee_output}\n"
|
108 |
|
|
|
|
|
|
|
|
|
|
|
109 |
cae_prompt = f"""
|
110 |
{mother_prompt}
|
111 |
-
شما Critical Analysis Expert (CAE) هستید. خروجی PEE رو نقد کن، مطمئن شو
|
112 |
خروجی PEE:\n{pee_output}
|
113 |
"""
|
114 |
cae_response = client.chat.completions.create(
|
@@ -123,18 +128,12 @@ def start_process(file1, file2, user_request, user_prompt):
|
|
123 |
**وضعیت فعلی:** پرامپت، پایگاه دانش و FAQ اولیه آماده شدن.
|
124 |
**اهداف بعدی:**
|
125 |
#G-{iteration_count}-1: بهبود بر اساس نقد CAE.
|
126 |
-
#G-{iteration_count}-2: تکمیل فرمت JSON یا بهینهسازی بیشتر.
|
127 |
**پایان iteration {iteration_count}**
|
128 |
"""
|
129 |
iteration_history += dce_summary
|
130 |
last_user_request = ""
|
131 |
|
132 |
-
|
133 |
-
prompt_output = parts[0]
|
134 |
-
knowledge_base_output = parts[1]
|
135 |
-
faq_output = parts[2]
|
136 |
-
|
137 |
-
return (iteration_history, prompt_output, knowledge_base_output, faq_output, "", f"Iteration {iteration_count}",
|
138 |
gr.update(interactive=False), gr.update(interactive=True))
|
139 |
|
140 |
# تابع ادامه Iteration
|
@@ -148,7 +147,7 @@ def continue_iteration(_=None):
|
|
148 |
if last_user_request:
|
149 |
iteration_history += f"**درخواست کاربر (فقط برای این Iteration):** {last_user_request}\n"
|
150 |
|
151 |
-
dce_instructions = f"iteration {iteration_count}: پرامپت قبلی رو بر اساس نقد CAE بهبود بده. اگه درخواست کاربر
|
152 |
iteration_history += f"**دستورات DCE:** {dce_instructions}\n"
|
153 |
|
154 |
pee_prompt = f"""
|
@@ -157,10 +156,10 @@ def continue_iteration(_=None):
|
|
157 |
اطلاعات کسبوکار: {json.dumps(business_info_serializable, ensure_ascii=False)}
|
158 |
اطلاعات محصولات: {json.dumps(product_info_serializable, ensure_ascii=False)}
|
159 |
پرامپت اولیه کاربر: {initial_prompt if initial_prompt else "هیچ پرامپتی وارد نشده"}
|
160 |
-
درخواست
|
161 |
تاریخچه iteration قبلی:\n{iteration_history}
|
162 |
{dce_instructions}
|
163 |
-
خروجی رو با --- جدا
|
164 |
"""
|
165 |
pee_response = client.chat.completions.create(
|
166 |
model="gpt-4o",
|
@@ -176,7 +175,7 @@ def continue_iteration(_=None):
|
|
176 |
|
177 |
cae_prompt = f"""
|
178 |
{mother_prompt}
|
179 |
-
شما Critical Analysis Expert (CAE) هستید. خروجی جدید PEE رو نقد کن و مطمئن شو
|
180 |
خروجی PEE:\n{pee_output}
|
181 |
"""
|
182 |
cae_response = client.chat.completions.create(
|
@@ -196,8 +195,8 @@ def continue_iteration(_=None):
|
|
196 |
iteration_history += dce_summary
|
197 |
last_user_request = ""
|
198 |
|
199 |
-
return (iteration_history, prompt_output, knowledge_base_output, faq_output, "",
|
200 |
-
|
201 |
|
202 |
# تابع پایان و تولید خروجی نهایی
|
203 |
def end_process(_=None):
|
@@ -212,15 +211,15 @@ def end_process(_=None):
|
|
212 |
final_prompt = f"""
|
213 |
{mother_prompt}
|
214 |
فرایند iterationها تموم شده. لطفاً خروجی نهایی رو تولید کن:
|
215 |
-
1. پرامپت چتبات به انگلیسی
|
216 |
-
2. پایگاه دانش به فرمت JSON-like
|
217 |
-
3. FAQ به فرمت JSON-like
|
218 |
اطلاعات کسبوکار: {json.dumps(business_info_serializable, ensure_ascii=False)}
|
219 |
اطلاعات محصولات: {json.dumps(product_info_serializable, ensure_ascii=False)}
|
220 |
پرامپت اولیه کاربر: {initial_prompt if initial_prompt else "هیچ پرامپتی وارد نشده"}
|
221 |
-
درخواست
|
222 |
تاریخچه iterationها:\n{iteration_history}
|
223 |
-
**هر بخش رو با
|
224 |
"""
|
225 |
final_response = client.chat.completions.create(
|
226 |
model="gpt-4o",
|
@@ -234,19 +233,11 @@ def end_process(_=None):
|
|
234 |
knowledge_base_output = parts[1]
|
235 |
faq_output = parts[2]
|
236 |
|
237 |
-
print("پرامپت نهایی:", prompt_output)
|
238 |
-
print("پایگاه دانش نهایی:", knowledge_base_output)
|
239 |
-
print("FAQ نهایی:", faq_output)
|
240 |
-
|
241 |
iteration_history += "\n**فرایند تموم شد و خروجی نهایی آمادهست! 🎉**\n"
|
242 |
last_user_request = ""
|
243 |
|
244 |
-
|
245 |
-
|
246 |
-
print("ارسال به تبها - FAQ:", faq_output)
|
247 |
-
|
248 |
-
return (iteration_history, prompt_output, knowledge_base_output, faq_output, "",
|
249 |
-
"فرایند پایان یافت", gr.update(interactive=True), gr.update(interactive=True))
|
250 |
|
251 |
# تابع ریست فرایند
|
252 |
def reset_process():
|
|
|
19 |
business_info = None
|
20 |
product_info = None
|
21 |
last_user_request = ""
|
22 |
+
initial_prompt = ""
|
23 |
|
24 |
# پرامپت مادر (بدون تغییر)
|
25 |
mother_prompt = """
|
|
|
84 |
iteration_history += f"**درخواست کاربر (فقط برای این Iteration):** {last_user_request}\n"
|
85 |
if initial_prompt:
|
86 |
iteration_history += f"**پرامپت اولیه کاربر:**\n{initial_prompt}\n"
|
87 |
+
prompt_output = initial_prompt
|
88 |
|
89 |
+
dce_instructions = f"iteration {iteration_count}: اگه پرامپت اولیه کاربر وجود داره، از اون بهعنوان پایه استفاده کن. اگه درخواست کاربر 'بهینه و خلاصه کن' بود، پرامپت رو مستقیم کوتاهتر کن و اطلاعات اضافی رو به پایگاه دانش یا FAQ منتقل کن بدون تغییر رفتار پرامپت. اگه درخواست تغییر پرامپته (مثل 'لحن رسمیتر بشه')، تغییرات رو اعمال کن. در غیر این صورت، یه پرامپت جدید به انگلیسی بساز."
|
90 |
iteration_history += f"**دستورات DCE:** {dce_instructions}\n"
|
91 |
|
92 |
pee_prompt = f"""
|
|
|
95 |
اطلاعات کسبوکار: {json.dumps(business_info_serializable, ensure_ascii=False)}
|
96 |
اطلاعات محصولات: {json.dumps(product_info_serializable, ensure_ascii=False)}
|
97 |
پرامپت اولیه کاربر: {initial_prompt if initial_prompt else "هیچ پرامپتی وارد نشده"}
|
98 |
+
درخواست کاربر: {last_user_request if last_user_request else "هیچ درخواستی وارد نشده"}
|
99 |
{dce_instructions}
|
100 |
+
خروجی رو با --- جدا کن (پرامپت --- پایگاه دانش --- FAQ).
|
101 |
"""
|
102 |
pee_response = client.chat.completions.create(
|
103 |
model="gpt-4o",
|
|
|
106 |
pee_output = pee_response.choices[0].message.content
|
107 |
iteration_history += f"**خروجی PEE:**\n{pee_output}\n"
|
108 |
|
109 |
+
parts = validate_and_fix_output(pee_output)
|
110 |
+
prompt_output = parts[0]
|
111 |
+
knowledge_base_output = parts[1]
|
112 |
+
faq_output = parts[2]
|
113 |
+
|
114 |
cae_prompt = f"""
|
115 |
{mother_prompt}
|
116 |
+
شما Critical Analysis Expert (CAE) هستید. خروجی PEE رو نقد کن، مطمئن شو درخواست کاربر رعایت شده:
|
117 |
خروجی PEE:\n{pee_output}
|
118 |
"""
|
119 |
cae_response = client.chat.completions.create(
|
|
|
128 |
**وضعیت فعلی:** پرامپت، پایگاه دانش و FAQ اولیه آماده شدن.
|
129 |
**اهداف بعدی:**
|
130 |
#G-{iteration_count}-1: بهبود بر اساس نقد CAE.
|
|
|
131 |
**پایان iteration {iteration_count}**
|
132 |
"""
|
133 |
iteration_history += dce_summary
|
134 |
last_user_request = ""
|
135 |
|
136 |
+
return (iteration_history, prompt_output, knowledge_base_output, faq_output, "", "", f"Iteration {iteration_count}",
|
|
|
|
|
|
|
|
|
|
|
137 |
gr.update(interactive=False), gr.update(interactive=True))
|
138 |
|
139 |
# تابع ادامه Iteration
|
|
|
147 |
if last_user_request:
|
148 |
iteration_history += f"**درخواست کاربر (فقط برای این Iteration):** {last_user_request}\n"
|
149 |
|
150 |
+
dce_instructions = f"iteration {iteration_count}: پرامپت قبلی رو بر اساس نقد CAE بهبود بده. اگه درخواست کاربر 'بهینه و خلاصه کن' بود، پرامپت رو مستقیم کوتاهتر کن و اطلاعات اضافی رو به پایگاه دانش یا FAQ منتقل کن بدون تغییر رفتار پرامپت. اگه درخواست تغییر پرامپته (مثل 'لحن دوستانهتر بشه')، تغییرات رو اعمال کن."
|
151 |
iteration_history += f"**دستورات DCE:** {dce_instructions}\n"
|
152 |
|
153 |
pee_prompt = f"""
|
|
|
156 |
اطلاعات کسبوکار: {json.dumps(business_info_serializable, ensure_ascii=False)}
|
157 |
اطلاعات محصولات: {json.dumps(product_info_serializable, ensure_ascii=False)}
|
158 |
پرامپت اولیه کاربر: {initial_prompt if initial_prompt else "هیچ پرامپتی وارد نشده"}
|
159 |
+
درخواست کاربر: {last_user_request if last_user_request else "هیچ درخواستی وارد نشده"}
|
160 |
تاریخچه iteration قبلی:\n{iteration_history}
|
161 |
{dce_instructions}
|
162 |
+
خروجی رو با --- جدا کن (پرامپت --- پایگاه دانش --- FAQ).
|
163 |
"""
|
164 |
pee_response = client.chat.completions.create(
|
165 |
model="gpt-4o",
|
|
|
175 |
|
176 |
cae_prompt = f"""
|
177 |
{mother_prompt}
|
178 |
+
شما Critical Analysis Expert (CAE) هستید. خروجی جدید PEE رو نقد کن و مطمئن شو درخواست کاربر رعایت شده:
|
179 |
خروجی PEE:\n{pee_output}
|
180 |
"""
|
181 |
cae_response = client.chat.completions.create(
|
|
|
195 |
iteration_history += dce_summary
|
196 |
last_user_request = ""
|
197 |
|
198 |
+
return (iteration_history, prompt_output, knowledge_base_output, faq_output, "", "", f"Iteration {iteration_count}",
|
199 |
+
gr.update(interactive=False), gr.update(interactive=True))
|
200 |
|
201 |
# تابع پایان و تولید خروجی نهایی
|
202 |
def end_process(_=None):
|
|
|
211 |
final_prompt = f"""
|
212 |
{mother_prompt}
|
213 |
فرایند iterationها تموم شده. لطفاً خروجی نهایی رو تولید کن:
|
214 |
+
1. پرامپت چتبات به انگلیسی (Persona, Tone, Guidelines, About Us, ...). اگه پرامپت اولیه کاربر وجود داره، از اون بهعنوان پایه استفاده کن. اگه درخواست بهینهسازی بود، پرامپت رو خلاصه کن و اطلاعات اضافی رو به پایگاه دانش یا FAQ منتقل کن بدون تغییر رفتار.
|
215 |
+
2. پایگاه دانش به فرمت JSON-like (name, description, variants, objectID).
|
216 |
+
3. FAQ به فرمت JSON-like (دستهبندیها، سوالات، جوابها).
|
217 |
اطلاعات کسبوکار: {json.dumps(business_info_serializable, ensure_ascii=False)}
|
218 |
اطلاعات محصولات: {json.dumps(product_info_serializable, ensure_ascii=False)}
|
219 |
پرامپت اولیه کاربر: {initial_prompt if initial_prompt else "هیچ پرامپتی وارد نشده"}
|
220 |
+
درخواست کاربر: {last_user_request if last_user_request else "هیچ درخواستی وارد نشده"}
|
221 |
تاریخچه iterationها:\n{iteration_history}
|
222 |
+
**هر بخش رو با --- جدا کن.**
|
223 |
"""
|
224 |
final_response = client.chat.completions.create(
|
225 |
model="gpt-4o",
|
|
|
233 |
knowledge_base_output = parts[1]
|
234 |
faq_output = parts[2]
|
235 |
|
|
|
|
|
|
|
|
|
236 |
iteration_history += "\n**فرایند تموم شد و خروجی نهایی آمادهست! 🎉**\n"
|
237 |
last_user_request = ""
|
238 |
|
239 |
+
return (iteration_history, prompt_output, knowledge_base_output, faq_output, "", "", "فرایند پایان یافت",
|
240 |
+
gr.update(interactive=True), gr.update(interactive=True))
|
|
|
|
|
|
|
|
|
241 |
|
242 |
# تابع ریست فرایند
|
243 |
def reset_process():
|