Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,27 +9,37 @@ client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
|
| 9 |
# Function to format the input into a strict JSON-based prompt
|
| 10 |
def format_prompt(topic, description, difficulty):
|
| 11 |
prompt = (
|
| 12 |
-
f"You are an expert educator. Generate highly engaging,
|
| 13 |
f"Use the following description as context: '{description}'. "
|
| 14 |
-
f"The content should be suitable for a '{difficulty}' difficulty level and MUST strictly
|
| 15 |
f"{{\n"
|
| 16 |
f" \"title\": \"[The title of the topic]\",\n"
|
| 17 |
f" \"sections\": [\n"
|
| 18 |
f" {{\n"
|
| 19 |
-
f" \"subheading\": \"[A clear subheading summarizing the section]\",\n"
|
| 20 |
-
f" \"content\": \"[
|
| 21 |
f" }}\n"
|
| 22 |
f" ]\n"
|
| 23 |
f"}}\n\n"
|
| 24 |
-
f"###
|
| 25 |
-
f"1.
|
| 26 |
-
f"2. Do not include
|
| 27 |
-
f"3. Ensure
|
|
|
|
|
|
|
|
|
|
| 28 |
f"4. Validate the JSON before returning it to ensure it is syntactically correct and complete.\n"
|
| 29 |
-
f"5. Do not
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
)
|
| 31 |
return prompt
|
| 32 |
|
|
|
|
| 33 |
# Function to clean and format the AI output
|
| 34 |
def clean_and_format_learning_content(output):
|
| 35 |
"""
|
|
|
|
| 9 |
# Function to format the input into a strict JSON-based prompt
|
| 10 |
def format_prompt(topic, description, difficulty):
|
| 11 |
prompt = (
|
| 12 |
+
f"You are an expert educator. Generate structured, highly engaging, and educational content on the topic '{topic}'. "
|
| 13 |
f"Use the following description as context: '{description}'. "
|
| 14 |
+
f"The content should be suitable for a '{difficulty}' difficulty level and MUST strictly follow this JSON structure:\n\n"
|
| 15 |
f"{{\n"
|
| 16 |
f" \"title\": \"[The title of the topic]\",\n"
|
| 17 |
f" \"sections\": [\n"
|
| 18 |
f" {{\n"
|
| 19 |
+
f" \"subheading\": \"[A clear and concise subheading summarizing the section]\",\n"
|
| 20 |
+
f" \"content\": \"[A detailed, engaging explanation of the section content, written in simple and accessible language.]\"\n"
|
| 21 |
f" }}\n"
|
| 22 |
f" ]\n"
|
| 23 |
f"}}\n\n"
|
| 24 |
+
f"### Strict Rules:\n"
|
| 25 |
+
f"1. Output must be a **valid JSON object** and nothing else.\n"
|
| 26 |
+
f"2. Do not include any additional text, comments, formatting instructions, or explanations.\n"
|
| 27 |
+
f"3. Ensure proper syntax:\n"
|
| 28 |
+
f" - All keys and string values must be enclosed in double quotes (\"\").\n"
|
| 29 |
+
f" - No trailing commas.\n"
|
| 30 |
+
f" - Properly close all braces (`}`) and brackets (`]`).\n"
|
| 31 |
f"4. Validate the JSON before returning it to ensure it is syntactically correct and complete.\n"
|
| 32 |
+
f"5. Do not include any extra characters, such as ◀, ▶, or ```.\n"
|
| 33 |
+
f"6. If there is insufficient data to generate meaningful content, generate a placeholder JSON object with empty values, e.g.,\n"
|
| 34 |
+
f"{{\n"
|
| 35 |
+
f" \"title\": \"\",\n"
|
| 36 |
+
f" \"sections\": []\n"
|
| 37 |
+
f"}}\n"
|
| 38 |
+
f"7. The response must always conform to this exact JSON structure, with no deviations."
|
| 39 |
)
|
| 40 |
return prompt
|
| 41 |
|
| 42 |
+
|
| 43 |
# Function to clean and format the AI output
|
| 44 |
def clean_and_format_learning_content(output):
|
| 45 |
"""
|