AneelaFatima commited on
Commit
06fb94c
·
verified ·
1 Parent(s): 9e0d47c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from openai import OpenAI
3
+ import gradio as gr
4
+
5
+ # Load API key from environment (for Hugging Face Secrets)
6
+ api_key = os.environ["GROQ_API_KEY"]
7
+
8
+ # Initialize OpenAI client for Groq
9
+ client = OpenAI(
10
+ api_key=api_key,
11
+ base_url="https://api.groq.com/openai/v1"
12
+ )
13
+
14
+ def generate_test_content(subject, chapter, topic, question_type, language):
15
+ try:
16
+ lang_instruction = "Answer in simple English." if language == "English" else "Answer in simple Urdu."
17
+
18
+ user_prompt = f"""
19
+ You are a test preparation tutor for Pakistani board students of Class 9 and 10.
20
+ Prepare content only from the specified syllabus.
21
+
22
+ Subject: {subject}
23
+ Chapter: {chapter}
24
+ Topic: {topic}
25
+ Question Type: {question_type}
26
+
27
+ {lang_instruction}
28
+
29
+ 1. Provide test preparation content from the given topic.
30
+ 2. If MCQs are selected, generate at least 5 MCQs with correct answers.
31
+ 3. If Short/Long Questions are selected, give proper answers based on syllabus.
32
+ 4. If Important Topics is selected, list 5 key concepts from this chapter.
33
+
34
+ Be friendly, encouraging, and never add off-topic material.
35
+ """
36
+
37
+ response = client.chat.completions.create(
38
+ model="llama3-70b-8192", # Supported model on Groq
39
+ messages=[
40
+ {"role": "system", "content": "You are a helpful exam preparation assistant."},
41
+ {"role": "user", "content": user_prompt}
42
+ ]
43
+ )
44
+
45
+ return response.choices[0].message.content
46
+
47
+ except Exception as e:
48
+ return f"\u274c Error: {str(e)}"
49
+
50
+ # Gradio Interface
51
+ interface = gr.Interface(
52
+ fn=generate_test_content,
53
+ inputs=[
54
+ gr.Textbox(label="\ud83d\udcd8 Subject (e.g., Biology)"),
55
+ gr.Textbox(label="\ud83d\udd22 Chapter (e.g., Chapter 2)"),
56
+ gr.Textbox(label="\ud83d\udccc Topic (e.g., Cell Structure)"),
57
+ gr.Radio(["MCQs", "Short Questions", "Long Questions", "Important Topics"], label="\u2753 Question Type"),
58
+ gr.Radio(["English", "Urdu"], label="\ud83c\udf10 Language")
59
+ ],
60
+ outputs="text",
61
+ title="\ud83e\udde0 TestPrep Guru \u2014 Syllabus-Based Exam Chatbot",
62
+ description="Get MCQs, short/long answers, and important topics based on your syllabus. Perfect for Class 9 & 10 students in Pakistan."
63
+ )
64
+
65
+ interface.launch()