Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -19,13 +19,37 @@ faq_output = ""
|
|
19 |
business_info = None
|
20 |
product_info = None
|
21 |
|
22 |
-
# پرامپت مادر
|
23 |
mother_prompt = """
|
24 |
You are the Nova System, an innovative problem-solving approach implemented by a dynamic consortium of virtual experts, each serving a distinct role. Your goal is to assist the user in generating high-quality prompts, a comprehensive knowledge base, and an automatically generated Frequently Asked Questions (FAQ) section for chatbots.
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
"""
|
27 |
|
28 |
-
# تابع برای تبدیل دادهها به فرمت JSON-serializable
|
29 |
def convert_to_serializable(obj):
|
30 |
if isinstance(obj, pd.Timestamp):
|
31 |
return obj.isoformat()
|
@@ -35,20 +59,25 @@ def convert_to_serializable(obj):
|
|
35 |
return {key: convert_to_serializable(value) for key, value in obj.items()}
|
36 |
return obj
|
37 |
|
38 |
-
# تابع برای اعتبارسنجی و اصلاح خروجی
|
39 |
def validate_and_fix_output(output):
|
40 |
print("خروجی خام مدل:", output)
|
41 |
parts = output.split("---")
|
42 |
parts = [part.strip() for part in parts if part.strip()]
|
43 |
print("بخشهای جدا شده:", parts)
|
|
|
|
|
44 |
if len(parts) > 3 and not parts[0].startswith("**Persona:") and not parts[0].startswith("["):
|
45 |
parts = parts[1:]
|
|
|
|
|
46 |
prompt_part = parts[0] if len(parts) > 0 else "پرامپت تولید نشد 😔"
|
47 |
kb_part = parts[1] if len(parts) > 1 else "پایگاه دانش تولید نشد 😕"
|
48 |
faq_part = parts[2] if len(parts) > 2 else "FAQ تولید نشد 🥳"
|
|
|
49 |
return [prompt_part, kb_part, faq_part]
|
50 |
|
51 |
-
# تابع برای خوندن و پردازش فایلهای اکسل
|
52 |
def process_excel_files(file1, file2):
|
53 |
global business_info, product_info
|
54 |
business_info = pd.read_excel(file1.name).to_dict(orient="records")[0] if file1 else {}
|
@@ -92,7 +121,7 @@ def start_process(file1, file2, user_request):
|
|
92 |
|
93 |
cae_prompt = f"""
|
94 |
{mother_prompt}
|
95 |
-
شما Critical Analysis Expert (CAE) هستید. خروجی PEE رو نقد کنید، مطمئن شید لحن
|
96 |
خروجی PEE:\n{pee_output}
|
97 |
"""
|
98 |
cae_response = client.chat.completions.create(
|
@@ -114,9 +143,9 @@ def start_process(file1, file2, user_request):
|
|
114 |
|
115 |
return iteration_history, "", "", "", "", ""
|
116 |
|
117 |
-
# تابع ادامه Iteration
|
118 |
def continue_iteration(user_request):
|
119 |
-
global iteration_count, iteration_history
|
120 |
iteration_count += 1
|
121 |
|
122 |
business_info_serializable = convert_to_serializable(business_info)
|
@@ -125,7 +154,7 @@ def continue_iteration(user_request):
|
|
125 |
if user_request:
|
126 |
iteration_history += f"**درخواست کاربر (فقط برای این Iteration):** {user_request}\n"
|
127 |
|
128 |
-
dce_instructions = f"iteration {iteration_count}: لطفاً خروجی قبلی رو بر اساس نقد CAE بهبود بدید، لحن رو دوستانهتر کنید و فرمت JSON رو دقیقتر کنید. اگه درخواست کاربر وجود داره، فقط توی این Iteration اعمالش کن."
|
129 |
iteration_history += f"**دستورات DCE:** {dce_instructions}\n"
|
130 |
|
131 |
pee_prompt = f"""
|
@@ -145,9 +174,15 @@ def continue_iteration(user_request):
|
|
145 |
pee_output = pee_response.choices[0].message.content
|
146 |
iteration_history += f"**خروجی PEE:**\n{pee_output}\n"
|
147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
cae_prompt = f"""
|
149 |
{mother_prompt}
|
150 |
-
شما Critical Analysis Expert (CAE) هستید. خروجی جدید PEE رو نقد کنید و مطمئن شید لحن
|
151 |
خروجی PEE:\n{pee_output}
|
152 |
"""
|
153 |
cae_response = client.chat.completions.create(
|
@@ -166,9 +201,9 @@ def continue_iteration(user_request):
|
|
166 |
"""
|
167 |
iteration_history += dce_summary
|
168 |
|
169 |
-
return iteration_history,
|
170 |
|
171 |
-
# تابع پایان و تولید خروجی نهایی
|
172 |
def end_process(user_request):
|
173 |
global iteration_history, prompt_output, knowledge_base_output, faq_output
|
174 |
|
@@ -189,7 +224,7 @@ def end_process(user_request):
|
|
189 |
- Responses to Common Questions
|
190 |
- Contact Information
|
191 |
- Additional Guidelines
|
192 |
-
لحن باید دوستانه، عامیانه، کوتاه (زیر 100 کلمه) و با ایموجیهای جذاب
|
193 |
---
|
194 |
2. پایگاه دانش به فرمت JSON-like با فیلدهای: name, description, variants (شامل size و price), objectID. برای هر محصول یا خدمت یه ورودی جدا بساز.
|
195 |
---
|
@@ -213,7 +248,7 @@ def end_process(user_request):
|
|
213 |
faq_output = parts[2]
|
214 |
|
215 |
iteration_history += "\n**فرایند تموم شد و خروجی نهایی آمادهست! 🎉**\n"
|
216 |
-
return iteration_history, prompt_output, knowledge_base_output, faq_output, ""
|
217 |
|
218 |
# رابط کاربری Gradio
|
219 |
with gr.Blocks() as demo:
|
@@ -229,7 +264,7 @@ with gr.Blocks() as demo:
|
|
229 |
prompt_box = gr.Textbox(label="پرامپت نهایی (انگلیسی)")
|
230 |
kb_box = gr.Textbox(label="پایگاه دانش (JSON)")
|
231 |
faq_box = gr.Textbox(label="پرسش و پاسخها (JSON)")
|
232 |
-
warning_box = gr.Textbox(label="هشدار", visible=False)
|
233 |
|
234 |
start_btn.click(
|
235 |
start_process,
|
|
|
19 |
business_info = None
|
20 |
product_info = None
|
21 |
|
22 |
+
# پرامپت مادر
|
23 |
mother_prompt = """
|
24 |
You are the Nova System, an innovative problem-solving approach implemented by a dynamic consortium of virtual experts, each serving a distinct role. Your goal is to assist the user in generating high-quality prompts, a comprehensive knowledge base, and an automatically generated Frequently Asked Questions (FAQ) section for chatbots.
|
25 |
+
|
26 |
+
**Nova System Process:**
|
27 |
+
The Nova System process is iterative and cyclical, involving the following key stages:
|
28 |
+
1. **Receiving and Processing User Information Forms:** Process the information from the Business Information Form and Product/Service Information Form provided by the user, if available.
|
29 |
+
2. **Processing User Requests:** Incorporate any additional user instructions or requests provided for the current iteration into the outputs. User requests are temporary and apply only to the current iteration unless specified otherwise.
|
30 |
+
3. **Assigning Expert Roles:**
|
31 |
+
- **Discussion Continuity Expert (DCE):** Manage and guide the process, provide instructions, summarize progress, and define goals for each iteration based on user requests and data.
|
32 |
+
- **Prompt Engineering Expert (PEE):** Generate initial drafts of:
|
33 |
+
- Chatbot prompt in English with sections: Persona, Tone, Guidelines, About Us, Responses to Common Questions, Contact Information, Additional Guidelines. Tone must be friendly, casual, concise (under 100 words per response unless necessary), and use loving emojis unless specified otherwise.
|
34 |
+
- Knowledge base in JSON-like format with fields: name, description, variants (size and price), objectID.
|
35 |
+
- FAQ in JSON-like format with categories, topics, questions, and short, friendly answers.
|
36 |
+
- **Critical Analysis Expert (CAE):** Review and critique outputs, ensuring they match the desired tone, structure, detail level, and user requests, providing improvement suggestions.
|
37 |
+
4. **Iterations and Expert Dialogue:** Conduct iterations with the following steps in Persian (Farsi):
|
38 |
+
- **DCE's Instructions:** Provide instructions for PEE and CAE based on user requests and previous outputs.
|
39 |
+
- **PEE Output:** Generate or refine chatbot prompt, knowledge base, and FAQ.
|
40 |
+
- **CAE Analysis:** Critique PEE outputs and suggest improvements.
|
41 |
+
- **DCE Summary:** Summarize progress and set goals for the next iteration.
|
42 |
+
5. **Iterate the Process:** Continue until high-quality outputs are achieved, incorporating user feedback or new instructions only for the current iteration unless specified.
|
43 |
+
6. **Present the Outputs:**
|
44 |
+
- Prompt in English with specified sections.
|
45 |
+
- Knowledge base in JSON-like format.
|
46 |
+
- FAQ in JSON-like format.
|
47 |
+
- Separate each section with exactly "---" (three dashes) and nothing else. **Do not add any extra text before, after, or between sections (e.g., no "Here is the output", no headings like "### Prompt:", no explanations).**
|
48 |
+
|
49 |
+
Conduct all dialogues in Persian, but output the prompt in English and knowledge base/FAQ in JSON-like format. Ensure the tone is friendly, casual, and uses loving emojis where appropriate, unless the user requests a different tone.
|
50 |
"""
|
51 |
|
52 |
+
# تابع برای تبدیل دادهها به فرمت JSON-serializable
|
53 |
def convert_to_serializable(obj):
|
54 |
if isinstance(obj, pd.Timestamp):
|
55 |
return obj.isoformat()
|
|
|
59 |
return {key: convert_to_serializable(value) for key, value in obj.items()}
|
60 |
return obj
|
61 |
|
62 |
+
# تابع برای اعتبارسنجی و اصلاح خروجی
|
63 |
def validate_and_fix_output(output):
|
64 |
print("خروجی خام مدل:", output)
|
65 |
parts = output.split("---")
|
66 |
parts = [part.strip() for part in parts if part.strip()]
|
67 |
print("بخشهای جدا شده:", parts)
|
68 |
+
|
69 |
+
# اگه متن اضافی قبل از اولین --- بود، اون رو نادیده بگیر
|
70 |
if len(parts) > 3 and not parts[0].startswith("**Persona:") and not parts[0].startswith("["):
|
71 |
parts = parts[1:]
|
72 |
+
|
73 |
+
# تخصیص بخشها بر اساس ترتیب
|
74 |
prompt_part = parts[0] if len(parts) > 0 else "پرامپت تولید نشد 😔"
|
75 |
kb_part = parts[1] if len(parts) > 1 else "پایگاه دانش تولید نشد 😕"
|
76 |
faq_part = parts[2] if len(parts) > 2 else "FAQ تولید نشد 🥳"
|
77 |
+
|
78 |
return [prompt_part, kb_part, faq_part]
|
79 |
|
80 |
+
# تابع برای خوندن و پردازش فایلهای اکسل
|
81 |
def process_excel_files(file1, file2):
|
82 |
global business_info, product_info
|
83 |
business_info = pd.read_excel(file1.name).to_dict(orient="records")[0] if file1 else {}
|
|
|
121 |
|
122 |
cae_prompt = f"""
|
123 |
{mother_prompt}
|
124 |
+
شما Critical Analysis Expert (CAE) هستید. خروجی PEE رو نقد کنید، مطمئن شید لحن درست اعمال شده، ساختار JSON درسته و درخواست کاربر (اگه هست) رعایت شده:
|
125 |
خروجی PEE:\n{pee_output}
|
126 |
"""
|
127 |
cae_response = client.chat.completions.create(
|
|
|
143 |
|
144 |
return iteration_history, "", "", "", "", ""
|
145 |
|
146 |
+
# تابع ادامه Iteration
|
147 |
def continue_iteration(user_request):
|
148 |
+
global iteration_count, iteration_history, prompt_output, knowledge_base_output, faq_output
|
149 |
iteration_count += 1
|
150 |
|
151 |
business_info_serializable = convert_to_serializable(business_info)
|
|
|
154 |
if user_request:
|
155 |
iteration_history += f"**درخواست کاربر (فقط برای این Iteration):** {user_request}\n"
|
156 |
|
157 |
+
dce_instructions = f"iteration {iteration_count}: لطفاً خروجی قبلی رو بر اساس نقد CAE بهبود بدید، لحن رو دوستانهتر کنید (مگر اینکه درخواست کاربر چیز دیگهای بگه) و فرمت JSON رو دقیقتر کنید. اگه درخواست کاربر وجود داره، فقط توی این Iteration اعمالش کن."
|
158 |
iteration_history += f"**دستورات DCE:** {dce_instructions}\n"
|
159 |
|
160 |
pee_prompt = f"""
|
|
|
174 |
pee_output = pee_response.choices[0].message.content
|
175 |
iteration_history += f"**خروجی PEE:**\n{pee_output}\n"
|
176 |
|
177 |
+
# پردازش خروجی PEE و بهروزرسانی فیلدها
|
178 |
+
parts = validate_and_fix_output(pee_output)
|
179 |
+
prompt_output = parts[0]
|
180 |
+
knowledge_base_output = parts[1]
|
181 |
+
faq_output = parts[2]
|
182 |
+
|
183 |
cae_prompt = f"""
|
184 |
{mother_prompt}
|
185 |
+
شما Critical Analysis Expert (CAE) هستید. خروجی جدید PEE رو نقد کنید و مطمئن شید لحن درست اعمال شده، فرمت JSON درسته و درخواست کاربر (اگه هست) رعایت شده:
|
186 |
خروجی PEE:\n{pee_output}
|
187 |
"""
|
188 |
cae_response = client.chat.completions.create(
|
|
|
201 |
"""
|
202 |
iteration_history += dce_summary
|
203 |
|
204 |
+
return iteration_history, prompt_output, knowledge_base_output, faq_output, "", ""
|
205 |
|
206 |
+
# تابع پایان و تولید خروجی نهایی
|
207 |
def end_process(user_request):
|
208 |
global iteration_history, prompt_output, knowledge_base_output, faq_output
|
209 |
|
|
|
224 |
- Responses to Common Questions
|
225 |
- Contact Information
|
226 |
- Additional Guidelines
|
227 |
+
لحن باید دوستانه، عامیانه، کوتاه (زیر 100 کلمه) و با ایموجیهای جذاب باشه مگر اینکه درخواست کاربر چیز دیگهای بگه.
|
228 |
---
|
229 |
2. پایگاه دانش به فرمت JSON-like با فیلدهای: name, description, variants (شامل size و price), objectID. برای هر محصول یا خدمت یه ورودی جدا بساز.
|
230 |
---
|
|
|
248 |
faq_output = parts[2]
|
249 |
|
250 |
iteration_history += "\n**فرایند تموم شد و خروجی نهایی آمادهست! 🎉**\n"
|
251 |
+
return iteration_history, prompt_output, knowledge_base_output, faq_output, "", ""
|
252 |
|
253 |
# رابط کاربری Gradio
|
254 |
with gr.Blocks() as demo:
|
|
|
264 |
prompt_box = gr.Textbox(label="پرامپت نهایی (انگلیسی)")
|
265 |
kb_box = gr.Textbox(label="پایگاه دانش (JSON)")
|
266 |
faq_box = gr.Textbox(label="پرسش و پاسخها (JSON)")
|
267 |
+
warning_box = gr.Textbox(label="هشدار", visible=False)
|
268 |
|
269 |
start_btn.click(
|
270 |
start_process,
|