Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,9 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import json
|
| 3 |
import google.generativeai as genai
|
| 4 |
-
import re
|
| 5 |
|
| 6 |
-
#
|
| 7 |
def test_ai_responses():
|
| 8 |
-
st.title("🤖 AI Response Debugger")
|
| 9 |
|
| 10 |
# Get API Key
|
| 11 |
api_key = st.text_input("Enter Gemini API Key:", type="password")
|
|
@@ -18,22 +16,17 @@ def test_ai_responses():
|
|
| 18 |
with tab1:
|
| 19 |
st.subheader("Test Study Plan Generation")
|
| 20 |
|
| 21 |
-
subject = st.text_input("Subject:", "Data Science")
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
hours_per_day = st.slider("Hours per Day:", 1, 8, 2)
|
| 25 |
-
study_days = st.multiselect("Study Days:", ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
|
| 26 |
-
default=["Mon", "Tue", "Wed", "Thu", "Fri"])
|
| 27 |
|
| 28 |
-
if st.button("Generate Test Study Plan"):
|
| 29 |
with st.spinner("Generating..."):
|
| 30 |
weeks = max(1, days_available // 7)
|
| 31 |
-
study_days_count = len(study_days)
|
| 32 |
|
| 33 |
prompt = f"""Create a detailed {weeks}-week study plan for: {subject}
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
Study schedule: {hours_per_day} hours/day, {study_days_count} days/week ({', '.join(study_days)})
|
| 37 |
|
| 38 |
IMPORTANT: Return JSON ONLY with this exact structure:
|
| 39 |
|
|
@@ -41,24 +34,24 @@ IMPORTANT: Return JSON ONLY with this exact structure:
|
|
| 41 |
"subject": "{subject}",
|
| 42 |
"total_weeks": {weeks},
|
| 43 |
"daily_hours": {hours_per_day},
|
| 44 |
-
"study_days":
|
| 45 |
-
"topics_allocation": {{"Topic1":
|
| 46 |
"weekly_schedule": [
|
| 47 |
{{
|
| 48 |
"week": 1,
|
| 49 |
"focus": "Week focus title",
|
| 50 |
"objectives": ["Objective 1", "Objective 2"],
|
| 51 |
"daily_tasks": [
|
| 52 |
-
["Task 1 for Monday", "Task 2 for Monday"
|
| 53 |
["Task 1 for Tuesday", "Task 2 for Tuesday"],
|
| 54 |
-
["Task 1 for Wednesday", "Task 2 for Wednesday"
|
| 55 |
-
["Task 1 for Thursday"],
|
| 56 |
["Task 1 for Friday", "Task 2 for Friday"]
|
| 57 |
],
|
| 58 |
"day_focus": ["Monday: Introduction", "Tuesday: Practice", "Wednesday: Deep Dive", "Thursday: Review", "Friday: Project"],
|
| 59 |
-
"week_summary": "Brief summary
|
| 60 |
"resources": ["Resource 1", "Resource 2"],
|
| 61 |
-
"milestone": "Weekly milestone
|
| 62 |
}}
|
| 63 |
],
|
| 64 |
"study_tips": ["Tip 1", "Tip 2"],
|
|
@@ -76,30 +69,29 @@ IMPORTANT: Return JSON ONLY with this exact structure:
|
|
| 76 |
)
|
| 77 |
|
| 78 |
raw_text = response.text
|
| 79 |
-
st.subheader("
|
| 80 |
-
st.text_area("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
# Try to extract JSON
|
| 83 |
-
st.subheader("Extracted JSON Attempt:")
|
| 84 |
-
extracted_json = extract_and_fix_json_test(raw_text, subject)
|
| 85 |
-
if extracted_json:
|
| 86 |
-
st.success("✅ JSON successfully extracted!")
|
| 87 |
-
st.json(extracted_json)
|
| 88 |
-
else:
|
| 89 |
-
st.error("❌ Failed to extract JSON")
|
| 90 |
-
st.text("Cleaned text for JSON extraction:")
|
| 91 |
-
st.text(clean_json_text(raw_text))
|
| 92 |
-
|
| 93 |
except Exception as e:
|
| 94 |
st.error(f"Error: {str(e)}")
|
| 95 |
|
| 96 |
with tab2:
|
| 97 |
st.subheader("Test Question Generation")
|
| 98 |
|
| 99 |
-
week_num = st.number_input("Week Number:", 1, 12, 1)
|
| 100 |
-
test_subject = st.text_input("Test Subject:", "Data Science")
|
| 101 |
|
| 102 |
-
if st.button("Generate Test Questions"):
|
| 103 |
with st.spinner("Generating test questions..."):
|
| 104 |
prompt = f"""Create 5 multiple choice questions for Week {week_num} of {test_subject}.
|
| 105 |
|
|
@@ -131,146 +123,21 @@ Return JSON:
|
|
| 131 |
)
|
| 132 |
|
| 133 |
raw_text = response.text
|
| 134 |
-
st.subheader("
|
| 135 |
-
st.text_area("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
# Try to extract JSON
|
| 138 |
-
st.subheader("Extracted JSON Attempt:")
|
| 139 |
-
extracted_json = extract_test_json_test(raw_text)
|
| 140 |
-
if extracted_json:
|
| 141 |
-
st.success("✅ JSON successfully extracted!")
|
| 142 |
-
st.json(extracted_json)
|
| 143 |
-
else:
|
| 144 |
-
st.error("❌ Failed to extract JSON")
|
| 145 |
-
st.text("Cleaned text for JSON extraction:")
|
| 146 |
-
st.text(clean_json_text(raw_text))
|
| 147 |
-
|
| 148 |
except Exception as e:
|
| 149 |
st.error(f"Error: {str(e)}")
|
| 150 |
|
| 151 |
-
# Test version of extract_and_fix_json
|
| 152 |
-
def extract_and_fix_json_test(text, subject="General"):
|
| 153 |
-
"""
|
| 154 |
-
Extract JSON from text and fix common issues - TEST VERSION
|
| 155 |
-
"""
|
| 156 |
-
if not text or not isinstance(text, str):
|
| 157 |
-
return None
|
| 158 |
-
|
| 159 |
-
# Clean the text
|
| 160 |
-
text = text.strip()
|
| 161 |
-
|
| 162 |
-
# Remove markdown code blocks
|
| 163 |
-
text = re.sub(r'```json\s*', '', text)
|
| 164 |
-
text = re.sub(r'```\s*', '', text)
|
| 165 |
-
|
| 166 |
-
# Find JSON boundaries
|
| 167 |
-
start_idx = text.find('{')
|
| 168 |
-
end_idx = text.rfind('}')
|
| 169 |
-
|
| 170 |
-
if start_idx == -1 or end_idx == -1 or end_idx <= start_idx:
|
| 171 |
-
return None
|
| 172 |
-
|
| 173 |
-
json_str = text[start_idx:end_idx+1]
|
| 174 |
-
|
| 175 |
-
try:
|
| 176 |
-
return json.loads(json_str)
|
| 177 |
-
except json.JSONDecodeError as e:
|
| 178 |
-
st.error(f"Initial JSON parse error: {str(e)}")
|
| 179 |
-
|
| 180 |
-
# Fix common JSON issues
|
| 181 |
-
# 1. Fix unquoted keys
|
| 182 |
-
json_str = re.sub(r'([{,])\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*:', r'\1"\2":', json_str)
|
| 183 |
-
|
| 184 |
-
# 2. Fix trailing commas
|
| 185 |
-
json_str = re.sub(r',\s*}', '}', json_str)
|
| 186 |
-
json_str = re.sub(r',\s*]', ']', json_str)
|
| 187 |
-
|
| 188 |
-
# 3. Fix single quotes to double quotes
|
| 189 |
-
json_str = re.sub(r"'([^']*)'", r'"\1"', json_str)
|
| 190 |
-
|
| 191 |
-
# 4. Fix missing commas between objects in arrays
|
| 192 |
-
json_str = re.sub(r'}\s*{', '},{', json_str)
|
| 193 |
-
|
| 194 |
-
try:
|
| 195 |
-
return json.loads(json_str)
|
| 196 |
-
except json.JSONDecodeError as e2:
|
| 197 |
-
st.error(f"Second JSON parse error: {str(e2)}")
|
| 198 |
-
|
| 199 |
-
# Try to fix unescaped quotes
|
| 200 |
-
json_str = json_str.replace('\\"', '"')
|
| 201 |
-
|
| 202 |
-
# Remove any non-ASCII characters if needed
|
| 203 |
-
json_str = json_str.encode('ascii', 'ignore').decode('ascii')
|
| 204 |
-
|
| 205 |
-
try:
|
| 206 |
-
return json.loads(json_str)
|
| 207 |
-
except:
|
| 208 |
-
return None
|
| 209 |
-
|
| 210 |
-
# Test version for test questions
|
| 211 |
-
def extract_test_json_test(text):
|
| 212 |
-
"""
|
| 213 |
-
Extract JSON from test response text
|
| 214 |
-
"""
|
| 215 |
-
if not text or not isinstance(text, str):
|
| 216 |
-
return None
|
| 217 |
-
|
| 218 |
-
text = text.strip()
|
| 219 |
-
|
| 220 |
-
# Remove markdown code blocks
|
| 221 |
-
text = re.sub(r'```json\s*', '', text)
|
| 222 |
-
text = re.sub(r'```\s*', '', text)
|
| 223 |
-
text = re.sub(r'```javascript\s*', '', text)
|
| 224 |
-
text = re.sub(r'```python\s*', '', text)
|
| 225 |
-
|
| 226 |
-
# Find the JSON part
|
| 227 |
-
start_idx = text.find('{')
|
| 228 |
-
end_idx = text.rfind('}')
|
| 229 |
-
|
| 230 |
-
if start_idx == -1 or end_idx == -1 or end_idx <= start_idx:
|
| 231 |
-
# Maybe it's an array
|
| 232 |
-
start_idx = text.find('[')
|
| 233 |
-
end_idx = text.rfind(']')
|
| 234 |
-
|
| 235 |
-
if start_idx == -1 or end_idx == -1 or end_idx <= start_idx:
|
| 236 |
-
return None
|
| 237 |
-
|
| 238 |
-
json_str = text[start_idx:end_idx+1]
|
| 239 |
-
|
| 240 |
-
try:
|
| 241 |
-
return json.loads(json_str)
|
| 242 |
-
except json.JSONDecodeError as e:
|
| 243 |
-
st.error(f"JSON parse error: {str(e)}")
|
| 244 |
-
|
| 245 |
-
# Common fixes
|
| 246 |
-
# Fix 1: Unquoted keys
|
| 247 |
-
json_str = re.sub(r'([{,])\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*:', r'\1"\2":', json_str)
|
| 248 |
-
|
| 249 |
-
# Fix 2: Single quotes to double quotes
|
| 250 |
-
json_str = re.sub(r"'([^']*)'", r'"\1"', json_str)
|
| 251 |
-
|
| 252 |
-
# Fix 3: Escape quotes in strings
|
| 253 |
-
json_str = json_str.replace('\\"', '\\\\"')
|
| 254 |
-
|
| 255 |
-
# Fix 4: Remove trailing commas
|
| 256 |
-
json_str = re.sub(r',\s*}', '}', json_str)
|
| 257 |
-
json_str = re.sub(r',\s*]', ']', json_str)
|
| 258 |
-
|
| 259 |
-
try:
|
| 260 |
-
return json.loads(json_str)
|
| 261 |
-
except:
|
| 262 |
-
return None
|
| 263 |
-
|
| 264 |
-
def clean_json_text(text):
|
| 265 |
-
"""
|
| 266 |
-
Clean text for JSON extraction
|
| 267 |
-
"""
|
| 268 |
-
text = text.strip()
|
| 269 |
-
text = re.sub(r'```json\s*', '', text)
|
| 270 |
-
text = re.sub(r'```\s*', '', text)
|
| 271 |
-
text = re.sub(r'```javascript\s*', '', text)
|
| 272 |
-
text = re.sub(r'```python\s*', '', text)
|
| 273 |
-
return text
|
| 274 |
-
|
| 275 |
# Run the test
|
| 276 |
test_ai_responses()
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import google.generativeai as genai
|
|
|
|
| 3 |
|
| 4 |
+
# Simple test function to show FULL raw AI responses
|
| 5 |
def test_ai_responses():
|
| 6 |
+
st.title("🤖 AI Response Debugger - RAW OUTPUT ONLY")
|
| 7 |
|
| 8 |
# Get API Key
|
| 9 |
api_key = st.text_input("Enter Gemini API Key:", type="password")
|
|
|
|
| 16 |
with tab1:
|
| 17 |
st.subheader("Test Study Plan Generation")
|
| 18 |
|
| 19 |
+
subject = st.text_input("Subject:", "Data Science", key="plan_subject")
|
| 20 |
+
days_available = st.slider("Days Available:", 7, 365, 60, key="plan_days")
|
| 21 |
+
hours_per_day = st.slider("Hours per Day:", 1, 8, 2, key="plan_hours")
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
if st.button("Generate Test Study Plan", key="plan_btn"):
|
| 24 |
with st.spinner("Generating..."):
|
| 25 |
weeks = max(1, days_available // 7)
|
|
|
|
| 26 |
|
| 27 |
prompt = f"""Create a detailed {weeks}-week study plan for: {subject}
|
| 28 |
+
|
| 29 |
+
Study schedule: {hours_per_day} hours/day, 5 days/week (Monday to Friday)
|
|
|
|
| 30 |
|
| 31 |
IMPORTANT: Return JSON ONLY with this exact structure:
|
| 32 |
|
|
|
|
| 34 |
"subject": "{subject}",
|
| 35 |
"total_weeks": {weeks},
|
| 36 |
"daily_hours": {hours_per_day},
|
| 37 |
+
"study_days": ["Mon", "Tue", "Wed", "Thu", "Fri"],
|
| 38 |
+
"topics_allocation": {{"Topic1": 10, "Topic2": 20, "Topic3": 30}},
|
| 39 |
"weekly_schedule": [
|
| 40 |
{{
|
| 41 |
"week": 1,
|
| 42 |
"focus": "Week focus title",
|
| 43 |
"objectives": ["Objective 1", "Objective 2"],
|
| 44 |
"daily_tasks": [
|
| 45 |
+
["Task 1 for Monday", "Task 2 for Monday"],
|
| 46 |
["Task 1 for Tuesday", "Task 2 for Tuesday"],
|
| 47 |
+
["Task 1 for Wednesday", "Task 2 for Wednesday"],
|
| 48 |
+
["Task 1 for Thursday", "Task 2 for Thursday"],
|
| 49 |
["Task 1 for Friday", "Task 2 for Friday"]
|
| 50 |
],
|
| 51 |
"day_focus": ["Monday: Introduction", "Tuesday: Practice", "Wednesday: Deep Dive", "Thursday: Review", "Friday: Project"],
|
| 52 |
+
"week_summary": "Brief summary",
|
| 53 |
"resources": ["Resource 1", "Resource 2"],
|
| 54 |
+
"milestone": "Weekly milestone"
|
| 55 |
}}
|
| 56 |
],
|
| 57 |
"study_tips": ["Tip 1", "Tip 2"],
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
raw_text = response.text
|
| 72 |
+
st.subheader("FULL RAW AI RESPONSE:")
|
| 73 |
+
st.text_area("COPY EVERYTHING FROM HERE:", raw_text, height=600)
|
| 74 |
+
|
| 75 |
+
# Show length info
|
| 76 |
+
st.info(f"Response length: {len(raw_text)} characters")
|
| 77 |
+
|
| 78 |
+
# Just show the raw text - NO PARSING ATTEMPT
|
| 79 |
+
st.markdown("---")
|
| 80 |
+
st.subheader("📋 What to do:")
|
| 81 |
+
st.write("1. Copy ALL the text from the box above")
|
| 82 |
+
st.write("2. Paste it in the chat for me to analyze")
|
| 83 |
+
st.write("3. I will create the correct JSON extraction function")
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
st.error(f"Error: {str(e)}")
|
| 87 |
|
| 88 |
with tab2:
|
| 89 |
st.subheader("Test Question Generation")
|
| 90 |
|
| 91 |
+
week_num = st.number_input("Week Number:", 1, 12, 1, key="test_week")
|
| 92 |
+
test_subject = st.text_input("Test Subject:", "Data Science", key="test_subject")
|
| 93 |
|
| 94 |
+
if st.button("Generate Test Questions", key="test_btn"):
|
| 95 |
with st.spinner("Generating test questions..."):
|
| 96 |
prompt = f"""Create 5 multiple choice questions for Week {week_num} of {test_subject}.
|
| 97 |
|
|
|
|
| 123 |
)
|
| 124 |
|
| 125 |
raw_text = response.text
|
| 126 |
+
st.subheader("FULL RAW AI RESPONSE:")
|
| 127 |
+
st.text_area("COPY EVERYTHING FROM HERE:", raw_text, height=600)
|
| 128 |
+
|
| 129 |
+
# Show length info
|
| 130 |
+
st.info(f"Response length: {len(raw_text)} characters")
|
| 131 |
+
|
| 132 |
+
# Just show the raw text - NO PARSING ATTEMPT
|
| 133 |
+
st.markdown("---")
|
| 134 |
+
st.subheader("📋 What to do:")
|
| 135 |
+
st.write("1. Copy ALL the text from the box above")
|
| 136 |
+
st.write("2. Paste it in the chat for me to analyze")
|
| 137 |
+
st.write("3. I will create the correct JSON extraction function")
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
except Exception as e:
|
| 140 |
st.error(f"Error: {str(e)}")
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
# Run the test
|
| 143 |
test_ai_responses()
|