Spaces:
Running
Running
fixes 2
Browse files
app.py
CHANGED
@@ -5,9 +5,14 @@ from datetime import datetime, timedelta
|
|
5 |
from collections import deque
|
6 |
import random
|
7 |
import logging
|
|
|
|
|
8 |
|
9 |
# Set up logging
|
10 |
-
logging.basicConfig(
|
|
|
|
|
|
|
11 |
logger = logging.getLogger(__name__)
|
12 |
|
13 |
# Initialize Anthropic client
|
@@ -56,6 +61,25 @@ def create_latex_document(content, questions_only=False):
|
|
56 |
logger.error(f"Error creating LaTeX document: {str(e)}")
|
57 |
raise
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
def generate_test(subject):
|
60 |
"""Generate a math test"""
|
61 |
try:
|
@@ -118,22 +142,13 @@ def generate_test(subject):
|
|
118 |
questions_latex = create_latex_document(response_text, questions_only=True)
|
119 |
full_latex = create_latex_document(response_text, questions_only=False)
|
120 |
|
121 |
-
#
|
122 |
-
|
123 |
-
|
124 |
-
"content": questions_latex.encode('utf-8'),
|
125 |
-
"mime_type": "text/plain"
|
126 |
-
}
|
127 |
-
|
128 |
-
full_file = {
|
129 |
-
"name": "full_test.tex",
|
130 |
-
"content": full_latex.encode('utf-8'),
|
131 |
-
"mime_type": "text/plain"
|
132 |
-
}
|
133 |
|
134 |
-
logger.debug("Successfully created
|
135 |
|
136 |
-
return response_text,
|
137 |
|
138 |
except Exception as e:
|
139 |
logger.error(f"Error generating test: {str(e)}")
|
@@ -176,13 +191,8 @@ with gr.Blocks() as interface:
|
|
176 |
questions_file = gr.File(label="Questions Only (LaTeX)")
|
177 |
full_file = gr.File(label="Full Test with Solutions (LaTeX)")
|
178 |
|
179 |
-
def generate_and_prepare_files(subject):
|
180 |
-
logger.debug("Starting test generation")
|
181 |
-
preview, questions_file_obj, full_file_obj = generate_test(subject)
|
182 |
-
return preview, questions_file_obj, full_file_obj
|
183 |
-
|
184 |
generate_btn.click(
|
185 |
-
|
186 |
inputs=[subject_dropdown],
|
187 |
outputs=[output_text, questions_file, full_file]
|
188 |
)
|
|
|
5 |
from collections import deque
|
6 |
import random
|
7 |
import logging
|
8 |
+
import tempfile
|
9 |
+
from pathlib import Path
|
10 |
|
11 |
# Set up logging
|
12 |
+
logging.basicConfig(
|
13 |
+
level=logging.DEBUG,
|
14 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
15 |
+
)
|
16 |
logger = logging.getLogger(__name__)
|
17 |
|
18 |
# Initialize Anthropic client
|
|
|
61 |
logger.error(f"Error creating LaTeX document: {str(e)}")
|
62 |
raise
|
63 |
|
64 |
+
def save_to_temp_file(content, filename):
|
65 |
+
"""Save content to a temporary file and return the path"""
|
66 |
+
try:
|
67 |
+
# Create a temporary directory that persists
|
68 |
+
temp_dir = Path(tempfile.gettempdir()) / "math_test_files"
|
69 |
+
temp_dir.mkdir(exist_ok=True)
|
70 |
+
|
71 |
+
# Create the file path
|
72 |
+
file_path = temp_dir / filename
|
73 |
+
|
74 |
+
# Write the content
|
75 |
+
file_path.write_text(content, encoding='utf-8')
|
76 |
+
|
77 |
+
logger.debug(f"Saved content to temporary file: {file_path}")
|
78 |
+
return str(file_path)
|
79 |
+
except Exception as e:
|
80 |
+
logger.error(f"Error saving temporary file: {str(e)}")
|
81 |
+
raise
|
82 |
+
|
83 |
def generate_test(subject):
|
84 |
"""Generate a math test"""
|
85 |
try:
|
|
|
142 |
questions_latex = create_latex_document(response_text, questions_only=True)
|
143 |
full_latex = create_latex_document(response_text, questions_only=False)
|
144 |
|
145 |
+
# Save to temporary files
|
146 |
+
questions_path = save_to_temp_file(questions_latex, "questions.tex")
|
147 |
+
full_path = save_to_temp_file(full_latex, "full_test.tex")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
|
149 |
+
logger.debug("Successfully created temporary files")
|
150 |
|
151 |
+
return response_text, questions_path, full_path
|
152 |
|
153 |
except Exception as e:
|
154 |
logger.error(f"Error generating test: {str(e)}")
|
|
|
191 |
questions_file = gr.File(label="Questions Only (LaTeX)")
|
192 |
full_file = gr.File(label="Full Test with Solutions (LaTeX)")
|
193 |
|
|
|
|
|
|
|
|
|
|
|
194 |
generate_btn.click(
|
195 |
+
generate_test,
|
196 |
inputs=[subject_dropdown],
|
197 |
outputs=[output_text, questions_file, full_file]
|
198 |
)
|