AneelaFatima commited on
Commit
8e0a0ac
Β·
verified Β·
1 Parent(s): 88ef6eb

Create app.py

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