File size: 9,644 Bytes
4973d3f
 
 
 
 
 
644ed04
4973d3f
 
 
 
644ed04
4973d3f
d947799
 
4973d3f
 
 
 
 
 
 
 
6039665
4973d3f
d947799
 
4973d3f
644ed04
 
7439c0c
 
 
 
 
 
644ed04
 
4973d3f
 
 
 
644ed04
 
4973d3f
7439c0c
4973d3f
 
644ed04
783d26c
d947799
 
4973d3f
d947799
 
 
 
 
 
 
4973d3f
 
 
 
644ed04
783d26c
4973d3f
 
 
 
 
 
d947799
4973d3f
 
644ed04
783d26c
d947799
4973d3f
d947799
4973d3f
d947799
4973d3f
 
644ed04
d947799
 
4973d3f
644ed04
 
7439c0c
 
 
 
 
 
 
 
 
 
 
 
644ed04
 
 
 
 
 
 
 
 
4973d3f
d947799
4973d3f
7439c0c
 
 
 
 
 
 
 
 
06832f3
7439c0c
06832f3
 
7439c0c
 
06832f3
7439c0c
 
06832f3
7439c0c
 
 
4e6c6b4
7439c0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446e104
7439c0c
828cb5c
7439c0c
 
 
 
828cb5c
7439c0c
 
 
 
828cb5c
7439c0c
 
4e6c6b4
d947799
 
 
 
 
 
 
 
 
 
 
7439c0c
d947799
 
 
 
7439c0c
 
 
d947799
7439c0c
d947799
7439c0c
4973d3f
d947799
7439c0c
 
 
 
 
d947799
c350dba
d947799
c350dba
c27c636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7439c0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
644ed04
d947799
 
c27c636
d947799
7439c0c
 
d947799
7439c0c
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import streamlit as st
from langchain_groq import ChatGroq
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from dotenv import load_dotenv
import pytesseract
from PIL import Image, ImageEnhance
import pdfplumber
import docx
from io import BytesIO
import logging
import os
from concurrent.futures import ThreadPoolExecutor
import requests
from bs4 import BeautifulSoup

# Load environment variables
load_dotenv()

# Initialize logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

# Initialize LLM
llm = ChatGroq(temperature=0.5, groq_api_key="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", model_name="llama3-8b-8192")

# OCR Configuration
pytesseract.pytesseract.tesseract_cmd = r"/usr/bin/tesseract"  # Adjust based on your system's path

# Function to enhance image for OCR processing
def enhance_image_for_ocr(image):
    # Convert to grayscale for better processing
    gray_image = image.convert("L")
    # Increase contrast for better text clarity
    enhancer = ImageEnhance.Contrast(gray_image)
    enhanced_image = enhancer.enhance(2.0)  # Increase contrast
    return enhanced_image

# Function to extract text from images using OCR
def extract_text_from_images(images, lang="eng"):
    ocr_text = ""
    for image in images:
        try:
            enhanced_image = enhance_image_for_ocr(image)
            ocr_text += pytesseract.image_to_string(enhanced_image, lang=lang).strip() + "\n"
        except Exception as e:
            logging.error(f"Error in OCR: {e}")
    return ocr_text.strip()

# Function to extract content from PDFs
@st.cache_data
def extract_pdf_data(pdf_file):
    data = {"text": "", "images": []}
    try:
        with pdfplumber.open(pdf_file) as pdf:
            for page in pdf.pages:
                data["text"] += page.extract_text() or ""
                for img in page.images:
                    base_image = pdf.extract_image(img["object_number"])
                    image = Image.open(BytesIO(base_image["image"]))
                    data["images"].append(image)
    except Exception as e:
        logging.error(f"Error processing PDF: {e}")
    return data

# Function to extract content from DOCX files
@st.cache_data
def extract_docx_data(docx_file):
    try:
        doc = docx.Document(docx_file)
        text = "\n".join([para.text.strip() for para in doc.paragraphs if para.text.strip()])
        return text
    except Exception as e:
        logging.error(f"Error processing DOCX: {e}")
        return ""

# Function to extract plain text from TXT files
@st.cache_data
def extract_txt_data(txt_file):
    try:
        return txt_file.read().decode("utf-8").strip()
    except Exception as e:
        logging.error(f"Error processing TXT: {e}")
        return ""

# Process uploaded files in parallel and extract text and images
def process_files(uploaded_files, lang="eng"):
    combined_text = ""
    images = []

    def process_file(file):
        file_type = file.type.split("/")[-1]
        if file_type == "pdf":
            pdf_data = extract_pdf_data(file)
            return pdf_data["text"], pdf_data["images"]
        elif file_type == "docx":
            return extract_docx_data(file), []
        elif file_type == "txt":
            return extract_txt_data(file), []
        elif file_type in ["png", "jpg", "jpeg"]:
            return "", [Image.open(file)]
        else:
            logging.error(f"Unsupported file type: {file_type}")
            return "", []

    with ThreadPoolExecutor() as executor:
        results = list(executor.map(process_file, uploaded_files))

    for text, img_list in results:
        combined_text += text
        images.extend(img_list)

    ocr_text = extract_text_from_images(images, lang)
    return combined_text + "\n" + ocr_text

# Function to generate questions
def generate_questions(question_type, syllabus_text, num_questions, difficulty, prompt_template):
    # Create a prompt based on user inputs
    prompt = prompt_template.format(
        num_questions=num_questions,
        question_type=question_type,
        syllabus_text=syllabus_text,
        **difficulty
    )

    # Pass the prompt to the LLM
    chain = (ChatPromptTemplate.from_template(prompt) | llm | StrOutputParser())
    try:
        questions = chain.invoke({})
        return questions
    except Exception as e:
        logging.error(f"Error generating questions: {e}")
        return ""

# Refined function to generate answers
def generate_answers(questions, syllabus_text):
    answers = {}
    
    for i, question in enumerate(questions.split("\n")):
        if question.strip():
            prompt = f"""
            Below is a syllabus excerpt. Please answer the following question based on the content provided. 
            Ensure the answer is directly related to the question and specific to the syllabus. 
            If necessary, explain key concepts clearly. Answer the question in a concise and detailed manner.

            Syllabus Content: {syllabus_text}

            Question: {question}
            Answer: 
            """

            chain = (ChatPromptTemplate.from_template(prompt) | llm | StrOutputParser())
            try:
                answer = chain.invoke({})
                answers[f"Answer {i+1}"] = answer.strip()
            except Exception as e:
                # Fall back to web search if LLM fails
                answers[f"Answer {i+1}"] = search_answers_online(question)
    
    return "\n".join([f"{k}: {v}" for k, v in answers.items()])

# Function to search answers online
def search_answers_online(question):
    search_url = f"https://www.google.com/search?q={question}"
    headers = {"User-Agent": "Mozilla/5.0"}
    try:
        response = requests.get(search_url, headers=headers)
        soup = BeautifulSoup(response.text, "html.parser")
        snippets = soup.find_all("div", class_="BNeawe")
        return "\n".join([snippet.get_text() for snippet in snippets[:3]])
    except Exception as e:
        logging.error(f"Error fetching online answers: {e}")
        return "No online answer found."

# Streamlit UI
st.title("AI-Powered Exam Generator")

# Tabs for navigation
tab1, tab2, tab3, tab4 = st.tabs(["πŸ“ Upload Files", "πŸ“„ Preview Content", "πŸ“ Generate Questions", "πŸ’‘ Generate Answers"])

# Upload files
with tab1:
    st.header("Upload Files")
    uploaded_files = st.file_uploader(
        "Upload your syllabus (PDF, DOCX, TXT, Images)",
        type=["pdf", "docx", "txt", "png", "jpg", "jpeg"],
        accept_multiple_files=True
    )
    ocr_lang = st.selectbox("Select OCR Language", ["eng", "spa", "fra", "deu", "ita"])
    if uploaded_files:
        syllabus_text = process_files(uploaded_files, lang=ocr_lang)
        st.session_state["syllabus_text"] = syllabus_text
        st.success("Files processed successfully!")

# Preview content
with tab2:
    st.header("Preview Syllabus Content")
    if "syllabus_text" in st.session_state:
        st.text_area("Extracted Content", st.session_state["syllabus_text"], height=300)
        if st.session_state.get("images"):
            for img in st.session_state["images"]:
                st.image(img, caption="Uploaded Image")
    else:
        st.warning("No content available. Upload files first.")

# Generate questions and answers
with tab3:
    st.header("Generate Questions and Answers")
    question_type = st.selectbox("Select Question Type", ["MCQs", "Short Questions", "Long Questions", "Fill-in-the-Blank", "Case Study"])
    num_questions = st.text_input("Total Number of Questions")
    difficulty_levels = ["Remember", "Understand", "Apply", "Analyze", "Evaluate", "Create"]
    difficulty = {level: st.slider(level, 0, 5, 1) for level in difficulty_levels}
    prompt_template = st.text_area(
        "Edit Prompt Template",
        """
        Generate {num_questions} {question_type} questions from the syllabus content below.
        Syllabus Content: {syllabus_text}
        Difficulty Levels:
        - Remember: {Remember}
        - Understand: {Understand}
        - Apply: {Apply}
        - Analyze: {Analyze}
        - Evaluate: {Evaluate}
        - Create: {Create}
        """,
        height=200
    )
    if num_questions.isdigit() and st.button("Generate Questions and Answers"):
        num_questions = int(num_questions)
        
        # Generate questions
        questions = generate_questions(question_type, st.session_state.get("syllabus_text", ""), num_questions, difficulty, prompt_template)
        st.session_state["questions"] = questions
        st.text_area("Generated Questions", questions, height=300)

        # Generate answers
        answers = generate_answers(questions, st.session_state.get("syllabus_text", ""))
        st.session_state["answers"] = answers
        st.text_area("Generated Answers", answers, height=300)

        # Download questions and answers
        st.download_button("Download Questions", questions, file_name="questions.txt")
        st.download_button("Download Answers", answers, file_name="answers.txt")

# Generate answers
with tab4:
    st.header("Generate Answers (Optional)")
    if "questions" in st.session_state:
        if st.button("Generate Answers"):
            answers = generate_answers(st.session_state["questions"], st.session_state.get("syllabus_text", ""))
            st.session_state["answers"] = answers
            st.text_area("Generated Answers", answers, height=300)

            # Download answers
            st.download_button("Download Answers", answers, file_name="answers.txt")