Turbiling commited on
Commit
45f88ad
·
verified ·
1 Parent(s): cebdff9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -25
app.py CHANGED
@@ -2,34 +2,46 @@ import os
2
  import gradio as gr
3
  from docx import Document
4
  import datetime
 
5
  from huggingface_hub import InferenceClient
6
 
7
- # ✅ Load your Hugging Face token from environment (set it in HF Spaces or locally)
 
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
 
10
- # ✅ Initialize Hugging Face client
11
- client = InferenceClient(
12
- model="tiiuae/falcon-7b-instruct",
13
- token=HF_TOKEN
14
- )
15
 
 
 
16
  def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher):
17
- if language == "Urdu":
18
- prompt = f"""
 
19
  آپ ایک تجربہ کار استاد ہیں۔
20
  براہ کرم درج ذیل موضوع پر ایک مکمل BOPPPS لیسن پلان اردو زبان میں تیار کریں۔
21
  موضوع: "{topic}"
22
- منصوبہ میں درج ذیل حصے شامل ہوں:
23
  1. Bridge-In: ایک مختصر دلچسپ تمہیدی پیراگراف۔
24
  2. 3 واضح تعلیمی مقاصد۔
25
  3. 2 قبل از جائزہ (Pre-Assessment) سوالات۔
26
  4. ایک مفصل تدریسی سرگرمی (Participatory Learning)۔
27
  5. 2 تا 3 بعد از جائزہ (Post-Assessment) سوالات۔
28
  6. سبق کا خلاصہ (Summary)۔
29
- براہ کرم متن کو قدرتی اور تعلیمی انداز میں اردو میں لکھیں۔
30
- """
31
- else:
32
- prompt = f"""
 
 
 
 
 
 
 
 
 
33
  You are a professional lesson plan expert.
34
  Create a detailed English lesson plan using the BOPPPS model for the topic: "{topic}".
35
  Do not repeat the basic info (class, subject, topic, teacher etc.) again in headings or body.
@@ -40,23 +52,20 @@ Include only the following:
40
  4. One detailed Participatory Learning Activity.
41
  5. 2–3 Post-Assessment Questions.
42
  6. A short Summary paragraph.
43
- """
44
 
45
- try:
46
- # ✅ Generate lesson text
47
- response = client.text_generation(
48
- prompt,
49
- max_new_tokens=800,
50
- temperature=0.7,
51
- )
52
 
53
- lesson_text = response.strip()
54
 
55
  # ✅ Create Word document
56
  doc = Document()
57
  doc.add_heading("BOPPPS Lesson Plan", 0)
58
 
59
- # Metadata table
60
  table = doc.add_table(rows=0, cols=2)
61
  data = {
62
  "Language": language,
@@ -85,7 +94,6 @@ Include only the following:
85
  return filepath
86
 
87
  except Exception as e:
88
- # Error handling
89
  error_doc = Document()
90
  error_doc.add_heading("Error in Lesson Plan Generator", 0)
91
  error_doc.add_paragraph(str(e))
@@ -127,4 +135,3 @@ with gr.Blocks() as demo:
127
 
128
  demo.launch()
129
 
130
-
 
2
  import gradio as gr
3
  from docx import Document
4
  import datetime
5
+ from groq import Groq
6
  from huggingface_hub import InferenceClient
7
 
8
+ # ✅ Load API Keys from environment variables (set in Hugging Face Secrets)
9
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
10
  HF_TOKEN = os.getenv("HF_TOKEN")
11
 
12
+ # ✅ Initialize clients
13
+ groq_client = Groq(api_key=GROQ_API_KEY)
14
+ hf_client = InferenceClient(model="tiiuae/falcon-7b-instruct", token=HF_TOKEN)
 
 
15
 
16
+
17
+ # ✅ Core Function
18
  def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher):
19
+ try:
20
+ if language == "Urdu":
21
+ prompt = f"""
22
  آپ ایک تجربہ کار استاد ہیں۔
23
  براہ کرم درج ذیل موضوع پر ایک مکمل BOPPPS لیسن پلان اردو زبان میں تیار کریں۔
24
  موضوع: "{topic}"
25
+ منصوبے میں درج ذیل حصے شامل ہوں:
26
  1. Bridge-In: ایک مختصر دلچسپ تمہیدی پیراگراف۔
27
  2. 3 واضح تعلیمی مقاصد۔
28
  3. 2 قبل از جائزہ (Pre-Assessment) سوالات۔
29
  4. ایک مفصل تدریسی سرگرمی (Participatory Learning)۔
30
  5. 2 تا 3 بعد از جائزہ (Post-Assessment) سوالات۔
31
  6. سبق کا خلاصہ (Summary)۔
32
+ براہ کرم مکمل متن قدرتی اور تعلیمی انداز میں اردو میں لکھیں۔
33
+ """
34
+
35
+ response = hf_client.text_generation(
36
+ prompt,
37
+ max_new_tokens=900,
38
+ temperature=0.7,
39
+ )
40
+
41
+ lesson_text = response.strip()
42
+
43
+ else:
44
+ prompt = f"""
45
  You are a professional lesson plan expert.
46
  Create a detailed English lesson plan using the BOPPPS model for the topic: "{topic}".
47
  Do not repeat the basic info (class, subject, topic, teacher etc.) again in headings or body.
 
52
  4. One detailed Participatory Learning Activity.
53
  5. 2–3 Post-Assessment Questions.
54
  6. A short Summary paragraph.
55
+ """
56
 
57
+ response = groq_client.chat.completions.create(
58
+ model="llama-3.1-8b-instant",
59
+ messages=[{"role": "user", "content": prompt}],
60
+ temperature=0.7,
61
+ )
 
 
62
 
63
+ lesson_text = response.choices[0].message.content.strip()
64
 
65
  # ✅ Create Word document
66
  doc = Document()
67
  doc.add_heading("BOPPPS Lesson Plan", 0)
68
 
 
69
  table = doc.add_table(rows=0, cols=2)
70
  data = {
71
  "Language": language,
 
94
  return filepath
95
 
96
  except Exception as e:
 
97
  error_doc = Document()
98
  error_doc.add_heading("Error in Lesson Plan Generator", 0)
99
  error_doc.add_paragraph(str(e))
 
135
 
136
  demo.launch()
137