Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,28 +9,36 @@ 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 structured, highly engaging, and educational
|
| 13 |
f"Use the following description as context: '{description}'. "
|
| 14 |
-
f"The content
|
| 15 |
f"{{\n"
|
| 16 |
-
f" \"title\": \"[
|
| 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
|
| 21 |
f" }}\n"
|
| 22 |
f" ]\n"
|
| 23 |
f"}}\n\n"
|
| 24 |
-
f"### Strict Rules:\n"
|
| 25 |
-
f"1. The output **must be a valid JSON object
|
| 26 |
-
f"2.
|
| 27 |
-
f"3.
|
| 28 |
-
f"4.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
)
|
| 30 |
return prompt
|
| 31 |
|
| 32 |
|
| 33 |
|
|
|
|
| 34 |
# Function to clean and format the AI output
|
| 35 |
def clean_and_format_learning_content(output):
|
| 36 |
"""
|
|
|
|
| 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 a structured, highly engaging, and educational JSON object on the topic '{topic}'. "
|
| 13 |
f"Use the following description as context: '{description}'. "
|
| 14 |
+
f"The content must be suitable for a '{difficulty}' difficulty level and strictly adhere to the following JSON structure:\n\n"
|
| 15 |
f"{{\n"
|
| 16 |
+
f" \"title\": \"[A descriptive and concise title for 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 clear, accessible language.]\"\n"
|
| 21 |
f" }}\n"
|
| 22 |
f" ]\n"
|
| 23 |
f"}}\n\n"
|
| 24 |
+
f"### Strict Output Rules:\n"
|
| 25 |
+
f"1. The output **must be a valid JSON object** and nothing else.\n"
|
| 26 |
+
f"2. All keys and string values must be enclosed in double quotes (\"\").\n"
|
| 27 |
+
f"3. The `sections` field must be a non-empty list of objects, each containing `subheading` and `content`.\n"
|
| 28 |
+
f"4. Avoid extra characters, trailing commas, or malformed syntax.\n"
|
| 29 |
+
f"5. Close all brackets and braces properly.\n"
|
| 30 |
+
f"6. If there is insufficient information, return a JSON object with empty placeholders, e.g.,\n"
|
| 31 |
+
f"{{\n"
|
| 32 |
+
f" \"title\": \"\",\n"
|
| 33 |
+
f" \"sections\": []\n"
|
| 34 |
+
f"}}\n"
|
| 35 |
+
f"7. Validate the output to ensure it complies with the required JSON structure.\n"
|
| 36 |
)
|
| 37 |
return prompt
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
+
|
| 42 |
# Function to clean and format the AI output
|
| 43 |
def clean_and_format_learning_content(output):
|
| 44 |
"""
|