Spaces:
Running
Running
Update app.py
#70
by Muthuraja18 - opened
app.py
CHANGED
|
@@ -145,13 +145,11 @@ questions_db = {
|
|
| 145 |
def generate_ai_questions(topic, num_questions=5):
|
| 146 |
"""
|
| 147 |
Generate AI-based questions for a given topic using Groq API.
|
| 148 |
-
|
| 149 |
-
[{"question": "...", "options": [...], "answer": "..."}]
|
| 150 |
-
Falls back to static questions if API fails.
|
| 151 |
"""
|
| 152 |
try:
|
| 153 |
GROQ_API_KEY = st.secrets["GROQ_API_KEY"].strip()
|
| 154 |
-
url = "https://api.groq.com/openai/v1/responses" # OpenAI-compatible
|
| 155 |
|
| 156 |
headers = {
|
| 157 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
|
@@ -170,7 +168,6 @@ def generate_ai_questions(topic, num_questions=5):
|
|
| 170 |
response.raise_for_status()
|
| 171 |
data = response.json()
|
| 172 |
|
| 173 |
-
# Groq may return output text under different fields
|
| 174 |
raw_text = data.get("output_text") or data.get("result") or ""
|
| 175 |
questions = []
|
| 176 |
|
|
@@ -186,14 +183,15 @@ def generate_ai_questions(topic, num_questions=5):
|
|
| 186 |
|
| 187 |
if not questions:
|
| 188 |
st.warning(f"No AI questions generated for topic '{topic}'. Using static questions if any.")
|
| 189 |
-
return
|
| 190 |
|
| 191 |
return questions
|
| 192 |
|
| 193 |
except Exception as e:
|
| 194 |
st.error(f"Groq API request failed: {e}")
|
| 195 |
st.warning(f"No AI questions generated for topic '{topic}'. Using static questions if any.")
|
| 196 |
-
return
|
|
|
|
| 197 |
|
| 198 |
|
| 199 |
def load_json(path, default):
|
|
|
|
| 145 |
def generate_ai_questions(topic, num_questions=5):
|
| 146 |
"""
|
| 147 |
Generate AI-based questions for a given topic using Groq API.
|
| 148 |
+
Falls back to `questions_db` if API fails.
|
|
|
|
|
|
|
| 149 |
"""
|
| 150 |
try:
|
| 151 |
GROQ_API_KEY = st.secrets["GROQ_API_KEY"].strip()
|
| 152 |
+
url = "https://api.groq.com/openai/v1/responses" # Groq OpenAI-compatible endpoint
|
| 153 |
|
| 154 |
headers = {
|
| 155 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
|
|
|
| 168 |
response.raise_for_status()
|
| 169 |
data = response.json()
|
| 170 |
|
|
|
|
| 171 |
raw_text = data.get("output_text") or data.get("result") or ""
|
| 172 |
questions = []
|
| 173 |
|
|
|
|
| 183 |
|
| 184 |
if not questions:
|
| 185 |
st.warning(f"No AI questions generated for topic '{topic}'. Using static questions if any.")
|
| 186 |
+
return questions_db.get(topic.lower(), questions_db.get("default", []))[:num_questions]
|
| 187 |
|
| 188 |
return questions
|
| 189 |
|
| 190 |
except Exception as e:
|
| 191 |
st.error(f"Groq API request failed: {e}")
|
| 192 |
st.warning(f"No AI questions generated for topic '{topic}'. Using static questions if any.")
|
| 193 |
+
return questions_db.get(topic.lower(), questions_db.get("default", []))[:num_questions]
|
| 194 |
+
|
| 195 |
|
| 196 |
|
| 197 |
def load_json(path, default):
|