Spaces:
Running
Running
Update app.py
#69
by
Muthuraja18
- opened
app.py
CHANGED
|
@@ -20,8 +20,6 @@ import random
|
|
| 20 |
from streamlit.components.v1 import html
|
| 21 |
import plotly.express as px
|
| 22 |
|
| 23 |
-
# ----------------- Groq AI Config -----------------
|
| 24 |
-
GROQ_MODEL = "llama3-70b-8192"
|
| 25 |
|
| 26 |
# Try to import firebase-admin (optional). If unavailable, app will run Offline.
|
| 27 |
try:
|
|
@@ -147,55 +145,55 @@ questions_db = {
|
|
| 147 |
def generate_ai_questions(topic, num_questions=5):
|
| 148 |
"""
|
| 149 |
Generate AI-based questions for a given topic using Groq API.
|
| 150 |
-
Returns a list of questions in format:
|
|
|
|
|
|
|
| 151 |
"""
|
| 152 |
-
|
|
|
|
|
|
|
| 153 |
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
"Content-Type": "application/json"
|
| 159 |
-
}
|
| 160 |
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
|
| 164 |
-
"prompt": prompt,
|
| 165 |
-
"max_tokens": 500,
|
| 166 |
-
"temperature": 0.7
|
| 167 |
-
}
|
| 168 |
-
|
| 169 |
-
try:
|
| 170 |
-
response = requests.post(url, headers=headers, json=payload, timeout=15)
|
| 171 |
response.raise_for_status()
|
| 172 |
data = response.json()
|
| 173 |
|
| 174 |
-
# Groq may return text
|
| 175 |
-
raw_text = data.get("
|
| 176 |
questions = []
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
parts
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
"answer": answer
|
| 189 |
-
})
|
| 190 |
if not questions:
|
| 191 |
st.warning(f"No AI questions generated for topic '{topic}'. Using static questions if any.")
|
|
|
|
|
|
|
| 192 |
return questions
|
| 193 |
|
| 194 |
-
except
|
| 195 |
st.error(f"Groq API request failed: {e}")
|
| 196 |
st.warning(f"No AI questions generated for topic '{topic}'. Using static questions if any.")
|
| 197 |
-
return []
|
| 198 |
-
|
| 199 |
|
| 200 |
|
| 201 |
def load_json(path, default):
|
|
|
|
| 20 |
from streamlit.components.v1 import html
|
| 21 |
import plotly.express as px
|
| 22 |
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Try to import firebase-admin (optional). If unavailable, app will run Offline.
|
| 25 |
try:
|
|
|
|
| 145 |
def generate_ai_questions(topic, num_questions=5):
|
| 146 |
"""
|
| 147 |
Generate AI-based questions for a given topic using Groq API.
|
| 148 |
+
Returns a list of questions in format:
|
| 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 Groq endpoint
|
| 155 |
|
| 156 |
+
headers = {
|
| 157 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 158 |
+
"Content-Type": "application/json"
|
| 159 |
+
}
|
| 160 |
|
| 161 |
+
prompt = f"Generate {num_questions} multiple-choice questions on the topic '{topic}'. " \
|
| 162 |
+
f"Return each question in this format:\nQuestion|Option1,Option2,Option3,Option4|Answer"
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
payload = {
|
| 165 |
+
"model": "openai/gpt-oss-20b",
|
| 166 |
+
"input": prompt
|
| 167 |
+
}
|
| 168 |
|
| 169 |
+
response = requests.post(url, headers=headers, json=payload, timeout=20)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
| 177 |
+
for line in raw_text.strip().split("\n"):
|
| 178 |
+
parts = line.strip().split("|")
|
| 179 |
+
if len(parts) == 3:
|
| 180 |
+
q_text = parts[0].strip()
|
| 181 |
+
opts = [o.strip() for o in parts[1].split(",")]
|
| 182 |
+
ans = parts[2].strip()
|
| 183 |
+
questions.append({"question": q_text, "options": opts, "answer": ans})
|
| 184 |
+
if len(questions) >= num_questions:
|
| 185 |
+
break
|
| 186 |
+
|
|
|
|
|
|
|
| 187 |
if not questions:
|
| 188 |
st.warning(f"No AI questions generated for topic '{topic}'. Using static questions if any.")
|
| 189 |
+
return STATIC_QUESTIONS.get(topic.lower(), STATIC_QUESTIONS["default"])[:num_questions]
|
| 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 STATIC_QUESTIONS.get(topic.lower(), STATIC_QUESTIONS["default"])[:num_questions]
|
|
|
|
| 197 |
|
| 198 |
|
| 199 |
def load_json(path, default):
|