ravi6389 commited on
Commit
582d365
·
verified ·
1 Parent(s): 073d6cf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from PIL import Image
4
+ # import pytesseract
5
+ from paddleocr import PaddleOCR, draw_ocr
6
+ from langchain.chains import LLMChain
7
+ from langchain.prompts import PromptTemplate
8
+ from langchain_groq import ChatGroq
9
+ import numpy as np
10
+ # import pytesseract
11
+ # import easyocr
12
+
13
+
14
+
15
+ GROQ_API_KEY = os.environ['GROQ_API_KEY ']
16
+ # llm = ChatGroq(temperature=0, groq_api_key=GROQ_API_KEY, model_name="mixtral-8x7b-32768")
17
+ llm = ChatGroq(temperature=0, groq_api_key=GROQ_API_KEY, model_name="llama3-groq-70b-8192-tool-use-preview")
18
+
19
+ ocr = PaddleOCR(use_angle_cls=True, lang='en')
20
+ # Function to extract text from an image using OCR
21
+ def extract_text_from_image(image):
22
+ try:
23
+ # Convert PIL Image to NumPy array
24
+ image_np = np.array(image)
25
+
26
+ # Perform OCR using PaddleOCR
27
+ results = ocr.ocr(image_np, cls=True)
28
+
29
+ # Extract text from results
30
+ extracted_text = ""
31
+ for line in results:
32
+ for word_info in line:
33
+ extracted_text += word_info[1][0] + " "
34
+
35
+ return extracted_text.strip() if extracted_text else "No text found in the image."
36
+
37
+ except Exception as e:
38
+ return f"Error processing image: {e}"
39
+ # try:
40
+ # # Use pytesseract to do OCR on the image
41
+ # # text = pytesseract.image_to_string(image)
42
+ # text = pytesseract.image_to_string(image, lang='eng', config='--psm 7')
43
+ # return text.strip()
44
+ # except Exception as e:
45
+ # return f"Error processing image: {e}"
46
+
47
+ # Function to solve a math problem using Groq API and Mistral LLM
48
+ def solve_math_problem_with_groq(problem_text):
49
+ try:
50
+ # Initialize the Groq LLM with the Mistral model
51
+
52
+ # Create a prompt template for solving the math problem
53
+ prompt_template = PromptTemplate(
54
+ input_variables=["problem"],
55
+ template="Solve the following math problem:\n\n{problem}\n\n"
56
+ )
57
+
58
+ # Create an LLM chain with the prompt template
59
+ llm_chain = LLMChain(
60
+ llm=llm,
61
+ prompt=prompt_template
62
+ )
63
+
64
+ # Run the LLM chain with the problem text
65
+ solution = llm_chain.run(problem=problem_text)
66
+ return solution.strip()
67
+ except Exception as e:
68
+ return f"Error solving the problem: {e}"
69
+
70
+ # Streamlit app
71
+ def main():
72
+ st.title("Math Solver using ChatGroq, Mistral, and LangChain")
73
+ st.write('Developed by Ravi Shankar Prasad- https://www.linkedin.com/in/ravi-shankar-prasad-371825101/')
74
+
75
+ # Input method selection
76
+ input_method = st.radio("Select input method:", ("Upload Image", "Enter Text"))
77
+
78
+ problem_text = ""
79
+
80
+ if input_method == "Upload Image":
81
+ # Image upload
82
+ uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
83
+ if uploaded_file is not None:
84
+ # Display uploaded image
85
+ image = Image.open(uploaded_file)
86
+ st.image(image, caption='Uploaded Image', use_column_width=True)
87
+ # Extract text from image
88
+ problem_text = extract_text_from_image(image)
89
+ if problem_text:
90
+ st.write("Extracted Text:", problem_text)
91
+ else:
92
+ st.write("No text found in the image. Please upload a clearer image.")
93
+ else:
94
+ # Text input
95
+ problem_text = st.text_area("Enter math problem:")
96
+
97
+ if problem_text:
98
+ if st.button("Solve"):
99
+ # Solve the math problem using Groq and Mistral
100
+ solution = solve_math_problem_with_groq(problem_text)
101
+ st.write("Solution:", solution)
102
+
103
+ if __name__ == "__main__":
104
+ main()