SyedAliHusnainGillani commited on
Commit
757a3cf
·
verified ·
1 Parent(s): c6fef5f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -42
app.py CHANGED
@@ -1,10 +1,10 @@
1
  import streamlit as st
2
  from sympy import sympify, Eq, solve
3
- from sympy.parsing.sympy_parser import transformations
4
  from PIL import Image
5
  import easyocr
6
- from transformers import pipeline
7
  import numpy as np
 
8
 
9
  # Initialize OCR reader and NLP models
10
  easyocr_reader = easyocr.Reader(['en'])
@@ -12,52 +12,50 @@ fill_blanks_model = pipeline("fill-mask", model="bert-base-uncased")
12
  qa_model = pipeline("question-answering")
13
 
14
  # Streamlit app title
15
- st.title("AI Quiz & Problem Solver")
16
- st.markdown("Solve mathematical problems, fill blanks, or answer MCQs from text or images.")
17
 
18
  # Sidebar options
19
  option = st.sidebar.selectbox("Select Input Type:", ("Text Input", "Image Upload"))
20
 
21
- def solve_problem(text):
22
- """Process and solve the problem based on its type."""
23
- try:
24
- st.subheader("Solution:")
25
-
26
- # Handle blanks (fill-in-the-blank questions)
27
- if "___" in text or "[MASK]" in text:
28
- st.write("Detected fill-in-the-blank question:")
29
- results = fill_blanks_model(text)
30
- for result in results:
31
- st.write(f"{result['sequence']} (Confidence: {result['score']:.2f})")
32
-
33
- # Handle MCQs
34
- elif "?" in text and any(option in text.lower() for option in ["a.", "b.", "c.", "d."]):
35
- st.write("Detected multiple-choice question:")
36
- question, *options = text.split("\n")
37
- options = [opt.strip() for opt in options if opt.strip()]
38
- answer = qa_model(question=question, context=" ".join(options))
39
- st.write(f"Answer: {answer['answer']} (Confidence: {answer['score']:.2f})")
40
-
41
-
42
- # Updated mathematical expression processing
43
- try:
44
- expr = sympify(user_input)
45
- if isinstance(expr, Eq):
46
- solution = solve(expr)
47
- st.write("Solutions:", solution)
48
- else:
49
- solution = solve(expr)
50
- st.write("Solution:", solution)
51
- except Exception as e:
52
- st.error("Could not process the input as a valid mathematical expression.")
53
-
54
-
55
  if option == "Text Input":
56
  # Text input
57
  user_input = st.text_area("Enter your question or problem:")
58
  if st.button("Solve"):
59
  if user_input.strip():
60
- solve_problem(user_input)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  else:
62
  st.error("Please input a valid text to solve.")
63
 
@@ -69,15 +67,43 @@ elif option == "Image Upload":
69
  st.image(image, caption="Uploaded Image", use_column_width=True)
70
 
71
  if st.button("Extract & Solve"):
 
72
  with st.spinner("Extracting text from image..."):
73
  try:
74
  extracted_text = easyocr_reader.readtext(np.array(image), detail=0)
75
- full_text = "\n".join(extracted_text)
76
  st.subheader("Extracted Text:")
 
77
  st.text(full_text)
78
- solve_problem(full_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  except Exception as e:
80
- st.error(f"Error extracting or solving text: {e}")
81
 
82
  # Additional Notes
83
  st.markdown("---")
 
1
  import streamlit as st
2
  from sympy import sympify, Eq, solve
3
+ import pytesseract
4
  from PIL import Image
5
  import easyocr
 
6
  import numpy as np
7
+ from transformers import pipeline
8
 
9
  # Initialize OCR reader and NLP models
10
  easyocr_reader = easyocr.Reader(['en'])
 
12
  qa_model = pipeline("question-answering")
13
 
14
  # Streamlit app title
15
+ st.title("Quiz and Numerical Problem Solver")
16
+ st.markdown("**Input a question or problem as text or upload an image, and get solutions!**")
17
 
18
  # Sidebar options
19
  option = st.sidebar.selectbox("Select Input Type:", ("Text Input", "Image Upload"))
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  if option == "Text Input":
22
  # Text input
23
  user_input = st.text_area("Enter your question or problem:")
24
  if st.button("Solve"):
25
  if user_input.strip():
26
+ try:
27
+ st.subheader("Solution:")
28
+
29
+ # Handle blanks (fill-in-the-blank questions)
30
+ if "___" in user_input or "[MASK]" in user_input:
31
+ st.write("Detected fill-in-the-blank question:")
32
+ results = fill_blanks_model(user_input)
33
+ for result in results:
34
+ st.write(f"{result['sequence']} (Confidence: {result['score']:.2f})")
35
+
36
+ # Handle MCQs
37
+ elif "?" in user_input and any(option in user_input.lower() for option in ["a.", "b.", "c.", "d."]):
38
+ st.write("Detected multiple-choice question:")
39
+ question, *options = user_input.split("\n")
40
+ options = [opt.strip() for opt in options if opt.strip()]
41
+ answer = qa_model(question=question, context=" ".join(options))
42
+ st.write(f"Answer: {answer['answer']} (Confidence: {answer['score']:.2f})")
43
+
44
+ # Handle mathematical expressions
45
+ else:
46
+ try:
47
+ expr = sympify(user_input) # Parse the input into a symbolic expression
48
+ if isinstance(expr, Eq): # If it's an equation, solve it
49
+ solution = solve(expr)
50
+ st.write("Solutions:", solution)
51
+ else: # Otherwise, solve the expression
52
+ solution = solve(expr)
53
+ st.write("Solution:", solution)
54
+ except Exception as e:
55
+ st.error(f"Error processing input: {str(e)}")
56
+
57
+ except Exception as e:
58
+ st.error("Error processing input. Please ensure it's a valid mathematical, quiz, or problem question.")
59
  else:
60
  st.error("Please input a valid text to solve.")
61
 
 
67
  st.image(image, caption="Uploaded Image", use_column_width=True)
68
 
69
  if st.button("Extract & Solve"):
70
+ # Extract text using OCR
71
  with st.spinner("Extracting text from image..."):
72
  try:
73
  extracted_text = easyocr_reader.readtext(np.array(image), detail=0)
 
74
  st.subheader("Extracted Text:")
75
+ full_text = "\n".join(extracted_text)
76
  st.text(full_text)
77
+
78
+ # Attempt to process the extracted text
79
+ st.subheader("Solution:")
80
+
81
+ # Handle blanks, MCQs, or math dynamically
82
+ if "___" in full_text or "[MASK]" in full_text:
83
+ st.write("Detected fill-in-the-blank question:")
84
+ results = fill_blanks_model(full_text)
85
+ for result in results:
86
+ st.write(f"{result['sequence']} (Confidence: {result['score']:.2f})")
87
+ elif "?" in full_text and any(option in full_text.lower() for option in ["a.", "b.", "c.", "d."]):
88
+ st.write("Detected multiple-choice question:")
89
+ question, *options = full_text.split("\n")
90
+ options = [opt.strip() for opt in options if opt.strip()]
91
+ answer = qa_model(question=question, context=" ".join(options))
92
+ st.write(f"Answer: {answer['answer']} (Confidence: {answer['score']:.2f})")
93
+ else:
94
+ try:
95
+ expr = sympify(full_text) # Parse the extracted text
96
+ if isinstance(expr, Eq): # If it's an equation, solve it
97
+ solution = solve(expr)
98
+ st.write("Solutions:", solution)
99
+ else: # Otherwise, solve the expression
100
+ solution = solve(expr)
101
+ st.write("Solution:", solution)
102
+ except Exception as e:
103
+ st.error(f"Error processing extracted text: {str(e)}")
104
+
105
  except Exception as e:
106
+ st.error("Error solving the problem from the extracted text. Ensure the image contains a valid problem.")
107
 
108
  # Additional Notes
109
  st.markdown("---")