Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,9 @@ import os
|
|
| 13 |
from concurrent.futures import ThreadPoolExecutor
|
| 14 |
import requests
|
| 15 |
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Load environment variables
|
| 18 |
load_dotenv()
|
|
@@ -28,9 +31,7 @@ pytesseract.pytesseract.tesseract_cmd = r"/usr/bin/tesseract" # Adjust based on
|
|
| 28 |
|
| 29 |
# Function to enhance image for OCR processing
|
| 30 |
def enhance_image_for_ocr(image):
|
| 31 |
-
# Convert to grayscale for better processing
|
| 32 |
gray_image = image.convert("L")
|
| 33 |
-
# Increase contrast for better text clarity
|
| 34 |
enhancer = ImageEnhance.Contrast(gray_image)
|
| 35 |
enhanced_image = enhancer.enhance(2.0) # Increase contrast
|
| 36 |
return enhanced_image
|
|
@@ -112,9 +113,8 @@ def process_files(uploaded_files, lang="eng"):
|
|
| 112 |
ocr_text = extract_text_from_images(images, lang)
|
| 113 |
return combined_text + "\n" + ocr_text
|
| 114 |
|
| 115 |
-
# Function to generate questions
|
| 116 |
def generate_questions(question_type, syllabus_text, num_questions, difficulty, prompt_template):
|
| 117 |
-
# Create a prompt based on user inputs
|
| 118 |
prompt = prompt_template.format(
|
| 119 |
num_questions=num_questions,
|
| 120 |
question_type=question_type,
|
|
@@ -122,16 +122,17 @@ def generate_questions(question_type, syllabus_text, num_questions, difficulty,
|
|
| 122 |
**difficulty
|
| 123 |
)
|
| 124 |
|
| 125 |
-
# Pass the prompt to the LLM
|
| 126 |
chain = (ChatPromptTemplate.from_template(prompt) | llm | StrOutputParser())
|
| 127 |
try:
|
| 128 |
questions = chain.invoke({})
|
| 129 |
-
|
|
|
|
|
|
|
| 130 |
except Exception as e:
|
| 131 |
logging.error(f"Error generating questions: {e}")
|
| 132 |
return ""
|
| 133 |
|
| 134 |
-
# Refined function to generate answers
|
| 135 |
def generate_answers(questions, syllabus_text):
|
| 136 |
answers = {}
|
| 137 |
|
|
@@ -153,7 +154,6 @@ def generate_answers(questions, syllabus_text):
|
|
| 153 |
answer = chain.invoke({})
|
| 154 |
answers[f"Answer {i+1}"] = answer.strip()
|
| 155 |
except Exception as e:
|
| 156 |
-
# Fall back to web search if LLM fails
|
| 157 |
answers[f"Answer {i+1}"] = search_answers_online(question)
|
| 158 |
|
| 159 |
return "\n".join([f"{k}: {v}" for k, v in answers.items()])
|
|
@@ -171,6 +171,22 @@ def search_answers_online(question):
|
|
| 171 |
logging.error(f"Error fetching online answers: {e}")
|
| 172 |
return "No online answer found."
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
# Streamlit UI
|
| 175 |
st.title("AI-Powered Exam Generator")
|
| 176 |
|
|
@@ -196,11 +212,6 @@ with tab2:
|
|
| 196 |
st.header("Preview Syllabus Content")
|
| 197 |
if "syllabus_text" in st.session_state:
|
| 198 |
st.text_area("Extracted Content", st.session_state["syllabus_text"], height=300)
|
| 199 |
-
if st.session_state.get("images"):
|
| 200 |
-
for img in st.session_state["images"]:
|
| 201 |
-
st.image(img, caption="Uploaded Image")
|
| 202 |
-
else:
|
| 203 |
-
st.warning("No content available. Upload files first.")
|
| 204 |
|
| 205 |
# Generate questions and answers
|
| 206 |
with tab3:
|
|
@@ -238,8 +249,10 @@ with tab3:
|
|
| 238 |
st.text_area("Generated Answers", answers, height=300)
|
| 239 |
|
| 240 |
# Download questions and answers
|
| 241 |
-
st.download_button("Download Questions", questions, file_name="questions.txt")
|
| 242 |
-
st.download_button("Download Answers", answers, file_name="answers.txt")
|
|
|
|
|
|
|
| 243 |
|
| 244 |
# Generate answers
|
| 245 |
with tab4:
|
|
|
|
| 13 |
from concurrent.futures import ThreadPoolExecutor
|
| 14 |
import requests
|
| 15 |
from bs4 import BeautifulSoup
|
| 16 |
+
import re
|
| 17 |
+
import json
|
| 18 |
+
import pandas as pd
|
| 19 |
|
| 20 |
# Load environment variables
|
| 21 |
load_dotenv()
|
|
|
|
| 31 |
|
| 32 |
# Function to enhance image for OCR processing
|
| 33 |
def enhance_image_for_ocr(image):
|
|
|
|
| 34 |
gray_image = image.convert("L")
|
|
|
|
| 35 |
enhancer = ImageEnhance.Contrast(gray_image)
|
| 36 |
enhanced_image = enhancer.enhance(2.0) # Increase contrast
|
| 37 |
return enhanced_image
|
|
|
|
| 113 |
ocr_text = extract_text_from_images(images, lang)
|
| 114 |
return combined_text + "\n" + ocr_text
|
| 115 |
|
| 116 |
+
# Function to generate questions (returning only the questions without metadata)
|
| 117 |
def generate_questions(question_type, syllabus_text, num_questions, difficulty, prompt_template):
|
|
|
|
| 118 |
prompt = prompt_template.format(
|
| 119 |
num_questions=num_questions,
|
| 120 |
question_type=question_type,
|
|
|
|
| 122 |
**difficulty
|
| 123 |
)
|
| 124 |
|
|
|
|
| 125 |
chain = (ChatPromptTemplate.from_template(prompt) | llm | StrOutputParser())
|
| 126 |
try:
|
| 127 |
questions = chain.invoke({})
|
| 128 |
+
# Extract only the question text, remove numbering, difficulty, etc.
|
| 129 |
+
questions_clean = re.sub(r"(\d+\.)|(.*?)", "", questions) # Remove question numbers and extra parts
|
| 130 |
+
return questions_clean.strip()
|
| 131 |
except Exception as e:
|
| 132 |
logging.error(f"Error generating questions: {e}")
|
| 133 |
return ""
|
| 134 |
|
| 135 |
+
# Refined function to generate answers (excluding question numbers and difficulty)
|
| 136 |
def generate_answers(questions, syllabus_text):
|
| 137 |
answers = {}
|
| 138 |
|
|
|
|
| 154 |
answer = chain.invoke({})
|
| 155 |
answers[f"Answer {i+1}"] = answer.strip()
|
| 156 |
except Exception as e:
|
|
|
|
| 157 |
answers[f"Answer {i+1}"] = search_answers_online(question)
|
| 158 |
|
| 159 |
return "\n".join([f"{k}: {v}" for k, v in answers.items()])
|
|
|
|
| 171 |
logging.error(f"Error fetching online answers: {e}")
|
| 172 |
return "No online answer found."
|
| 173 |
|
| 174 |
+
# Additional Function to Download Questions/Answers in JSON Format
|
| 175 |
+
def save_as_json(questions, answers):
|
| 176 |
+
data = {
|
| 177 |
+
"questions": questions,
|
| 178 |
+
"answers": answers
|
| 179 |
+
}
|
| 180 |
+
return json.dumps(data)
|
| 181 |
+
|
| 182 |
+
# Additional Function to Download Questions/Answers in CSV Format
|
| 183 |
+
def save_as_csv(questions, answers):
|
| 184 |
+
df = pd.DataFrame({
|
| 185 |
+
"Questions": questions.split("\n"),
|
| 186 |
+
"Answers": answers.split("\n")
|
| 187 |
+
})
|
| 188 |
+
return df.to_csv(index=False)
|
| 189 |
+
|
| 190 |
# Streamlit UI
|
| 191 |
st.title("AI-Powered Exam Generator")
|
| 192 |
|
|
|
|
| 212 |
st.header("Preview Syllabus Content")
|
| 213 |
if "syllabus_text" in st.session_state:
|
| 214 |
st.text_area("Extracted Content", st.session_state["syllabus_text"], height=300)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
# Generate questions and answers
|
| 217 |
with tab3:
|
|
|
|
| 249 |
st.text_area("Generated Answers", answers, height=300)
|
| 250 |
|
| 251 |
# Download questions and answers
|
| 252 |
+
st.download_button("Download Questions (TXT)", questions, file_name="questions.txt")
|
| 253 |
+
st.download_button("Download Answers (TXT)", answers, file_name="answers.txt")
|
| 254 |
+
st.download_button("Download Q&A (JSON)", save_as_json(questions, answers), file_name="qa.json")
|
| 255 |
+
st.download_button("Download Q&A (CSV)", save_as_csv(questions, answers), file_name="qa.csv")
|
| 256 |
|
| 257 |
# Generate answers
|
| 258 |
with tab4:
|