Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,41 +12,72 @@ from docx_builder import (
|
|
| 12 |
build_obe_docx,
|
| 13 |
)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
#
|
| 17 |
-
#
|
| 18 |
-
def
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
def init_clients():
|
| 30 |
groq_key = os.getenv("GROQ_API_KEY")
|
| 31 |
serp_key = os.getenv("SERPAPI_KEY")
|
|
|
|
| 32 |
groq = GroqClient(api_key=groq_key)
|
| 33 |
serp = SerpClient(api_key=serp_key)
|
|
|
|
| 34 |
return groq, serp
|
| 35 |
|
|
|
|
| 36 |
groq_client, serp_client = init_clients()
|
| 37 |
orchestrator = MultiAgentOrchestrator(groq_client, serp_client)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
#
|
| 41 |
-
#
|
|
|
|
| 42 |
def run_system(subject, stream, partA, partB, partC, syl_file, ref_file):
|
| 43 |
|
| 44 |
if syl_file is None:
|
| 45 |
-
return None, None, None, "Upload syllabus
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
|
|
|
|
| 50 |
output = orchestrator.run_pipeline(
|
| 51 |
subject=subject,
|
| 52 |
stream=stream,
|
|
@@ -60,46 +91,52 @@ def run_system(subject, stream, partA, partB, partC, syl_file, ref_file):
|
|
| 60 |
final_json = output.get("final", {})
|
| 61 |
generator_raw = output.get("generator_raw", "")
|
| 62 |
|
|
|
|
| 63 |
tmpdir = Path(tempfile.mkdtemp())
|
| 64 |
-
qp_path = tmpdir / f"{subject}_QP.docx"
|
| 65 |
-
ans_path = tmpdir / f"{subject}_Answers.docx"
|
| 66 |
-
obe_path = tmpdir / f"{subject}_OBE.docx"
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
build_question_paper_docx(qp_path, final_json, generator_raw, subject)
|
| 69 |
build_answers_docx(ans_path, final_json, subject)
|
| 70 |
build_obe_docx(obe_path, final_json, subject)
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
#
|
| 76 |
-
#
|
| 77 |
with gr.Blocks() as app:
|
| 78 |
-
|
|
|
|
| 79 |
|
| 80 |
with gr.Row():
|
| 81 |
-
subject = gr.Textbox(label="Subject Name")
|
| 82 |
-
stream = gr.Dropdown(["CSE", "Non-CSE"],
|
| 83 |
|
| 84 |
with gr.Row():
|
| 85 |
-
partA = gr.Number(value=10, precision=0
|
| 86 |
-
partB = gr.Number(value=5, precision=0
|
| 87 |
-
partC = gr.Number(value=1, precision=0
|
| 88 |
|
| 89 |
-
syllabus = gr.File(label="Upload Syllabus
|
| 90 |
ref_qp = gr.File(label="Upload Reference QP (Optional)")
|
| 91 |
|
| 92 |
-
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
status = gr.Markdown("Status:
|
| 98 |
|
| 99 |
-
|
| 100 |
run_system,
|
| 101 |
inputs=[subject, stream, partA, partB, partC, syllabus, ref_qp],
|
| 102 |
-
outputs=[
|
| 103 |
)
|
| 104 |
|
| 105 |
app.launch()
|
|
|
|
| 12 |
build_obe_docx,
|
| 13 |
)
|
| 14 |
|
| 15 |
+
# =====================================================
|
| 16 |
+
# SAFE FILE TEXT EXTRACTION (TXT, DOCX, PDF Supported)
|
| 17 |
+
# =====================================================
|
| 18 |
+
def extract_text(file_obj):
|
| 19 |
+
"""Reads syllabus from .txt, .docx or .pdf without breaking."""
|
| 20 |
+
name = file_obj.name.lower()
|
| 21 |
+
|
| 22 |
+
# TXT
|
| 23 |
+
if name.endswith(".txt"):
|
| 24 |
+
content = file_obj.read()
|
| 25 |
+
try:
|
| 26 |
+
return content.decode("utf-8", errors="ignore")
|
| 27 |
+
except:
|
| 28 |
+
return content.decode("latin-1", errors="ignore")
|
| 29 |
+
|
| 30 |
+
# DOCX
|
| 31 |
+
if name.endswith(".docx"):
|
| 32 |
+
try:
|
| 33 |
+
from docx import Document
|
| 34 |
+
doc = Document(file_obj.name)
|
| 35 |
+
return "\n".join([p.text for p in doc.paragraphs])
|
| 36 |
+
except:
|
| 37 |
+
return ""
|
| 38 |
+
|
| 39 |
+
# PDF
|
| 40 |
+
if name.endswith(".pdf"):
|
| 41 |
+
try:
|
| 42 |
+
reader = PdfReader(file_obj.name)
|
| 43 |
+
pages = [p.extract_text() or "" for p in reader.pages]
|
| 44 |
+
return "\n".join(pages)
|
| 45 |
+
except Exception:
|
| 46 |
+
return ""
|
| 47 |
+
|
| 48 |
+
return ""
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# =====================================================
|
| 52 |
+
# INITIALIZE CLIENTS
|
| 53 |
+
# =====================================================
|
| 54 |
def init_clients():
|
| 55 |
groq_key = os.getenv("GROQ_API_KEY")
|
| 56 |
serp_key = os.getenv("SERPAPI_KEY")
|
| 57 |
+
|
| 58 |
groq = GroqClient(api_key=groq_key)
|
| 59 |
serp = SerpClient(api_key=serp_key)
|
| 60 |
+
|
| 61 |
return groq, serp
|
| 62 |
|
| 63 |
+
|
| 64 |
groq_client, serp_client = init_clients()
|
| 65 |
orchestrator = MultiAgentOrchestrator(groq_client, serp_client)
|
| 66 |
|
| 67 |
+
|
| 68 |
+
# =====================================================
|
| 69 |
+
# MAIN PIPELINE FUNCTION
|
| 70 |
+
# =====================================================
|
| 71 |
def run_system(subject, stream, partA, partB, partC, syl_file, ref_file):
|
| 72 |
|
| 73 |
if syl_file is None:
|
| 74 |
+
return None, None, None, "Error: Upload a syllabus file."
|
| 75 |
|
| 76 |
+
# Extract text safely
|
| 77 |
+
syllabus_text = extract_text(syl_file)
|
| 78 |
+
ref_text = extract_text(ref_file) if ref_file else ""
|
| 79 |
|
| 80 |
+
# Run multi-agent orchestration
|
| 81 |
output = orchestrator.run_pipeline(
|
| 82 |
subject=subject,
|
| 83 |
stream=stream,
|
|
|
|
| 91 |
final_json = output.get("final", {})
|
| 92 |
generator_raw = output.get("generator_raw", "")
|
| 93 |
|
| 94 |
+
# Temporary directory for docx exports
|
| 95 |
tmpdir = Path(tempfile.mkdtemp())
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
qp_path = tmpdir / f"{subject}_QuestionPaper.docx"
|
| 98 |
+
ans_path = tmpdir / f"{subject}_AnswerKey.docx"
|
| 99 |
+
obe_path = tmpdir / f"{subject}_OBE_Summary.docx"
|
| 100 |
+
|
| 101 |
+
# Build all DOCX files
|
| 102 |
build_question_paper_docx(qp_path, final_json, generator_raw, subject)
|
| 103 |
build_answers_docx(ans_path, final_json, subject)
|
| 104 |
build_obe_docx(obe_path, final_json, subject)
|
| 105 |
|
| 106 |
+
# RETURN FILE PATHS AS STRINGS (required by Gradio)
|
| 107 |
+
return str(qp_path), str(ans_path), str(obe_path), "Generation Complete!"
|
| 108 |
+
|
| 109 |
|
| 110 |
+
# =====================================================
|
| 111 |
+
# GRADIO USER INTERFACE
|
| 112 |
+
# =====================================================
|
| 113 |
with gr.Blocks() as app:
|
| 114 |
+
|
| 115 |
+
gr.Markdown("## Multi-Agent Question Paper Generator (Groq + SerpAPI)")
|
| 116 |
|
| 117 |
with gr.Row():
|
| 118 |
+
subject = gr.Textbox(label="Subject Name", placeholder="Cloud Computing")
|
| 119 |
+
stream = gr.Dropdown(["CSE", "Non-CSE"], label="Stream", value="CSE")
|
| 120 |
|
| 121 |
with gr.Row():
|
| 122 |
+
partA = gr.Number(label="Part A Questions", value=10, precision=0)
|
| 123 |
+
partB = gr.Number(label="Part B Questions (Either/Or)", value=5, precision=0)
|
| 124 |
+
partC = gr.Number(label="Part C Questions", value=1, precision=0)
|
| 125 |
|
| 126 |
+
syllabus = gr.File(label="Upload Syllabus (.pdf / .txt / .docx)")
|
| 127 |
ref_qp = gr.File(label="Upload Reference QP (Optional)")
|
| 128 |
|
| 129 |
+
generate_btn = gr.Button("Generate Question Paper")
|
| 130 |
|
| 131 |
+
qp_output = gr.File(label="Download Question Paper")
|
| 132 |
+
ans_output = gr.File(label="Download Answer Key")
|
| 133 |
+
obe_output = gr.File(label="Download OBE Summary")
|
| 134 |
+
status = gr.Markdown("Status: Waiting...")
|
| 135 |
|
| 136 |
+
generate_btn.click(
|
| 137 |
run_system,
|
| 138 |
inputs=[subject, stream, partA, partB, partC, syllabus, ref_qp],
|
| 139 |
+
outputs=[qp_output, ans_output, obe_output, status]
|
| 140 |
)
|
| 141 |
|
| 142 |
app.launch()
|