Turbiling commited on
Commit
7d3c5aa
·
verified ·
1 Parent(s): 6d090e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -24
app.py CHANGED
@@ -7,17 +7,13 @@ import datetime
7
  from groq import Groq
8
  from huggingface_hub import InferenceClient
9
 
10
- # Load API keys
11
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
12
  HF_TOKEN = os.getenv("HF_TOKEN")
13
 
14
- # Initialize clients
15
  groq_client = Groq(api_key=GROQ_API_KEY)
16
- hf_client = InferenceClient(api_key=HF_TOKEN)
17
 
18
- # -----------------------------
19
- # Urdu + English Lesson Plan Generator
20
- # -----------------------------
21
  def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher):
22
  try:
23
  if language == "English":
@@ -33,7 +29,6 @@ Include the following structured components only:
33
  5. 2–3 Post-Assessment Questions.
34
  6. Summary paragraph by the teacher.
35
  """
36
-
37
  response = groq_client.chat.completions.create(
38
  model="llama-3.1-8b-instant",
39
  messages=[{"role": "user", "content": prompt}],
@@ -53,25 +48,21 @@ Include the following structured components only:
53
  5. 2 تا 3 بعد از تدریس سوالات (Post-assessment)
54
  6. سبق کا خلاصہ (Summary)
55
  """
56
-
57
  urdu_response = hf_client.text_generation(
58
- model="MuhammadYaseen77999/urdu-islamic-bot",
59
  prompt=prompt,
60
- max_new_tokens=600,
61
- temperature=0.7,
62
  )
63
-
64
- # Extract Urdu text properly
65
  if isinstance(urdu_response, str):
66
  lesson_text = urdu_response.strip()
67
  else:
68
  lesson_text = urdu_response.get("generated_text", "").strip()
69
 
70
- # Create Word document
71
  doc = Document()
72
  doc.add_heading("BOPPPS Lesson Plan", 0)
73
 
74
- # Metadata table
75
  table = doc.add_table(rows=0, cols=2)
76
  data = {
77
  "Class": class_,
@@ -82,18 +73,15 @@ Include the following structured components only:
82
  "Date": date,
83
  "Teacher Name": teacher
84
  }
85
-
86
  for key, value in data.items():
87
  row_cells = table.add_row().cells
88
  row_cells[0].text = key
89
- row_cells[1].text = value
90
 
91
  doc.add_paragraph("\n")
92
-
93
- # Add right-to-left formatting for Urdu text
94
  paragraph = doc.add_paragraph(lesson_text)
95
  if language == "Urdu":
96
- paragraph.alignment = 2 # Right alignment
97
  rtl = OxmlElement('w:rtl')
98
  rtl.set(qn('w:val'), '1')
99
  paragraph._element.insert(0, rtl)
@@ -107,7 +95,6 @@ Include the following structured components only:
107
  return filepath
108
 
109
  except Exception as e:
110
- # Return error file
111
  error_doc = Document()
112
  error_doc.add_heading("Error in Lesson Plan Generator", 0)
113
  error_doc.add_paragraph(str(e))
@@ -117,9 +104,7 @@ Include the following structured components only:
117
  return filepath
118
 
119
 
120
- # -----------------------------
121
  # Gradio UI
122
- # -----------------------------
123
  with gr.Blocks() as demo:
124
  gr.Markdown("## 🌍 Bilingual BOPPPS Lesson Plan Generator (English + Urdu)")
125
 
@@ -149,4 +134,3 @@ with gr.Blocks() as demo:
149
  gr.Markdown("🔹 Developed by **Najaf Ali Sharqi**")
150
 
151
  demo.launch()
152
-
 
7
  from groq import Groq
8
  from huggingface_hub import InferenceClient
9
 
10
+ # Load API keys (must set in Secrets / environment)
11
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
12
  HF_TOKEN = os.getenv("HF_TOKEN")
13
 
 
14
  groq_client = Groq(api_key=GROQ_API_KEY)
15
+ hf_client = InferenceClient(token=HF_TOKEN)
16
 
 
 
 
17
  def generate_lesson_plan(language, class_, subject, topic, students, duration, date, teacher):
18
  try:
19
  if language == "English":
 
29
  5. 2–3 Post-Assessment Questions.
30
  6. Summary paragraph by the teacher.
31
  """
 
32
  response = groq_client.chat.completions.create(
33
  model="llama-3.1-8b-instant",
34
  messages=[{"role": "user", "content": prompt}],
 
48
  5. 2 تا 3 بعد از تدریس سوالات (Post-assessment)
49
  6. سبق کا خلاصہ (Summary)
50
  """
 
51
  urdu_response = hf_client.text_generation(
52
+ model="Imran1/gpt2-urdu-news",
53
  prompt=prompt,
54
+ max_new_tokens=500,
55
+ temperature=0.8,
56
  )
 
 
57
  if isinstance(urdu_response, str):
58
  lesson_text = urdu_response.strip()
59
  else:
60
  lesson_text = urdu_response.get("generated_text", "").strip()
61
 
62
+ # Create the Word document
63
  doc = Document()
64
  doc.add_heading("BOPPPS Lesson Plan", 0)
65
 
 
66
  table = doc.add_table(rows=0, cols=2)
67
  data = {
68
  "Class": class_,
 
73
  "Date": date,
74
  "Teacher Name": teacher
75
  }
 
76
  for key, value in data.items():
77
  row_cells = table.add_row().cells
78
  row_cells[0].text = key
79
+ row_cells[1].text = str(value)
80
 
81
  doc.add_paragraph("\n")
 
 
82
  paragraph = doc.add_paragraph(lesson_text)
83
  if language == "Urdu":
84
+ paragraph.alignment = 2 # right align
85
  rtl = OxmlElement('w:rtl')
86
  rtl.set(qn('w:val'), '1')
87
  paragraph._element.insert(0, rtl)
 
95
  return filepath
96
 
97
  except Exception as e:
 
98
  error_doc = Document()
99
  error_doc.add_heading("Error in Lesson Plan Generator", 0)
100
  error_doc.add_paragraph(str(e))
 
104
  return filepath
105
 
106
 
 
107
  # Gradio UI
 
108
  with gr.Blocks() as demo:
109
  gr.Markdown("## 🌍 Bilingual BOPPPS Lesson Plan Generator (English + Urdu)")
110
 
 
134
  gr.Markdown("🔹 Developed by **Najaf Ali Sharqi**")
135
 
136
  demo.launch()