added support for content length
Browse files- app/main.py +17 -4
- spaces_app.py +9 -3
app/main.py
CHANGED
|
@@ -45,6 +45,7 @@ class ContentRequest(BaseModel):
|
|
| 45 |
subject: str
|
| 46 |
topic: str
|
| 47 |
style: str = "normal"
|
|
|
|
| 48 |
|
| 49 |
TOPIC_KEYWORDS = {
|
| 50 |
# Grade 3 Science
|
|
@@ -282,7 +283,14 @@ def generate_response_with_rag(prompt, index, embedding_model, documents, settin
|
|
| 282 |
"normal": ""
|
| 283 |
}
|
| 284 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
instruction = style_instructions.get(settings.get("style", "normal"), "")
|
|
|
|
| 286 |
|
| 287 |
# Create system prompt
|
| 288 |
system_prompt = f"""
|
|
@@ -297,9 +305,9 @@ def generate_response_with_rag(prompt, index, embedding_model, documents, settin
|
|
| 297 |
**Respond to all questions and instructions in Swahili.**
|
| 298 |
|
| 299 |
Please follow these instructions:
|
| 300 |
-
* Fully written-out numbers (e.g.,
|
| 301 |
-
* No abbreviations (e.g.,
|
| 302 |
-
* Swahili-only words or phonetic
|
| 303 |
* Minimal punctuation—just commas and full stops
|
| 304 |
* Quotations, brackets, symbols removed
|
| 305 |
* Natural, spoken tone fit for young learners
|
|
@@ -307,6 +315,7 @@ def generate_response_with_rag(prompt, index, embedding_model, documents, settin
|
|
| 307 |
IMPORTANT: Make sure at least two [Picture: Image description] are included in the response.
|
| 308 |
|
| 309 |
{instruction}
|
|
|
|
| 310 |
|
| 311 |
Context:
|
| 312 |
{retrieved_context}
|
|
@@ -514,6 +523,9 @@ async def generate_content(request: ContentRequest):
|
|
| 514 |
if request.style not in ["normal", "simple", "creative"]:
|
| 515 |
raise HTTPException(status_code=400, detail="Invalid style. Must be 'normal', 'simple', or 'creative'")
|
| 516 |
|
|
|
|
|
|
|
|
|
|
| 517 |
# Get relevant topic files
|
| 518 |
topic_files = get_topic_files(request.grade, request.subject, request.topic)
|
| 519 |
if not topic_files:
|
|
@@ -524,7 +536,8 @@ async def generate_content(request: ContentRequest):
|
|
| 524 |
"style": request.style,
|
| 525 |
"topic": request.topic,
|
| 526 |
"grade": request.grade,
|
| 527 |
-
"subject": request.subject
|
|
|
|
| 528 |
}
|
| 529 |
|
| 530 |
response = generate_response_with_rag(
|
|
|
|
| 45 |
subject: str
|
| 46 |
topic: str
|
| 47 |
style: str = "normal"
|
| 48 |
+
content_length: str = "medium"
|
| 49 |
|
| 50 |
TOPIC_KEYWORDS = {
|
| 51 |
# Grade 3 Science
|
|
|
|
| 283 |
"normal": ""
|
| 284 |
}
|
| 285 |
|
| 286 |
+
content_length_instructions = {
|
| 287 |
+
"short": "Keep your response brief and concise. Focus on the most essential points only. Provide only 2-3 subtopics, 1-2 activities, and 3-4 practice questions.",
|
| 288 |
+
"medium": "", # Default length, no special instructions
|
| 289 |
+
"long": "Provide a comprehensive and detailed explanation. Include more examples, detailed explanations for each subtopic (at least 4-5 subtopics), 3-4 activities, and 6-8 practice questions."
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
instruction = style_instructions.get(settings.get("style", "normal"), "")
|
| 293 |
+
length_instruction = content_length_instructions.get(settings.get("content_length", "medium"), "")
|
| 294 |
|
| 295 |
# Create system prompt
|
| 296 |
system_prompt = f"""
|
|
|
|
| 305 |
**Respond to all questions and instructions in Swahili.**
|
| 306 |
|
| 307 |
Please follow these instructions:
|
| 308 |
+
* Fully written-out numbers (e.g., "watoto watatu" instead of "3")
|
| 309 |
+
* No abbreviations (e.g., "Shule ya Msingi" not "Sh. ya Msingi")
|
| 310 |
+
* Swahili-only words or phonetic "Swahilicized" versions for foreign terms
|
| 311 |
* Minimal punctuation—just commas and full stops
|
| 312 |
* Quotations, brackets, symbols removed
|
| 313 |
* Natural, spoken tone fit for young learners
|
|
|
|
| 315 |
IMPORTANT: Make sure at least two [Picture: Image description] are included in the response.
|
| 316 |
|
| 317 |
{instruction}
|
| 318 |
+
{length_instruction}
|
| 319 |
|
| 320 |
Context:
|
| 321 |
{retrieved_context}
|
|
|
|
| 523 |
if request.style not in ["normal", "simple", "creative"]:
|
| 524 |
raise HTTPException(status_code=400, detail="Invalid style. Must be 'normal', 'simple', or 'creative'")
|
| 525 |
|
| 526 |
+
if request.content_length not in ["short", "medium", "long"]:
|
| 527 |
+
raise HTTPException(status_code=400, detail="Invalid content length. Must be 'short', 'medium', or 'long'")
|
| 528 |
+
|
| 529 |
# Get relevant topic files
|
| 530 |
topic_files = get_topic_files(request.grade, request.subject, request.topic)
|
| 531 |
if not topic_files:
|
|
|
|
| 536 |
"style": request.style,
|
| 537 |
"topic": request.topic,
|
| 538 |
"grade": request.grade,
|
| 539 |
+
"subject": request.subject,
|
| 540 |
+
"content_length": request.content_length
|
| 541 |
}
|
| 542 |
|
| 543 |
response = generate_response_with_rag(
|
spaces_app.py
CHANGED
|
@@ -36,14 +36,15 @@ asyncio.set_event_loop(loop)
|
|
| 36 |
loop.run_until_complete(startup_event())
|
| 37 |
|
| 38 |
# Define the Gradio interface
|
| 39 |
-
def generate(grade, subject, topic, style):
|
| 40 |
try:
|
| 41 |
# Create a ContentRequest object
|
| 42 |
request = ContentRequest(
|
| 43 |
grade=int(grade),
|
| 44 |
subject=subject,
|
| 45 |
topic=topic,
|
| 46 |
-
style=style
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
# Call the generate_content function
|
|
@@ -77,6 +78,11 @@ with gr.Blocks(title="Swahili Content Generation") as demo:
|
|
| 77 |
label="Style",
|
| 78 |
value="normal"
|
| 79 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
generate_btn = gr.Button("Generate Content")
|
| 81 |
|
| 82 |
with gr.Column():
|
|
@@ -84,7 +90,7 @@ with gr.Blocks(title="Swahili Content Generation") as demo:
|
|
| 84 |
|
| 85 |
generate_btn.click(
|
| 86 |
fn=generate,
|
| 87 |
-
inputs=[grade, subject, topic, style],
|
| 88 |
outputs=output
|
| 89 |
)
|
| 90 |
|
|
|
|
| 36 |
loop.run_until_complete(startup_event())
|
| 37 |
|
| 38 |
# Define the Gradio interface
|
| 39 |
+
def generate(grade, subject, topic, style, content_length):
|
| 40 |
try:
|
| 41 |
# Create a ContentRequest object
|
| 42 |
request = ContentRequest(
|
| 43 |
grade=int(grade),
|
| 44 |
subject=subject,
|
| 45 |
topic=topic,
|
| 46 |
+
style=style,
|
| 47 |
+
content_length=content_length
|
| 48 |
)
|
| 49 |
|
| 50 |
# Call the generate_content function
|
|
|
|
| 78 |
label="Style",
|
| 79 |
value="normal"
|
| 80 |
)
|
| 81 |
+
content_length = gr.Dropdown(
|
| 82 |
+
choices=["short", "medium", "long"],
|
| 83 |
+
label="Content Length",
|
| 84 |
+
value="medium"
|
| 85 |
+
)
|
| 86 |
generate_btn = gr.Button("Generate Content")
|
| 87 |
|
| 88 |
with gr.Column():
|
|
|
|
| 90 |
|
| 91 |
generate_btn.click(
|
| 92 |
fn=generate,
|
| 93 |
+
inputs=[grade, subject, topic, style, content_length],
|
| 94 |
outputs=output
|
| 95 |
)
|
| 96 |
|