sharma-kanishka commited on
Commit
74b0513
·
verified ·
1 Parent(s): c9a5d33

Upload 3 files

Browse files

Working files added

Files changed (3) hide show
  1. app.py +87 -0
  2. requirements.txt +6 -0
  3. utils.py +41 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ load_dotenv()
3
+ from utils import encode_image_to_base64
4
+ import os
5
+ from langchain_groq.chat_models import ChatGroq
6
+ from langchain_groq.chat_models import ChatGroq
7
+ from langchain_core.messages import HumanMessage
8
+ import streamlit as st
9
+
10
+ def analyse(file):
11
+ api_key = os.getenv("GROQ_API_KEY")
12
+ if not api_key:
13
+ raise ValueError("GROQ_API_KEY not found")
14
+
15
+ print("Encoding image...")
16
+ encoded_file, mime_type = encode_image_to_base64(file)
17
+ print(f"Image encoded successfully. MIME type: {mime_type}")
18
+
19
+ print("Initializing ChatGroq...")
20
+ llm = ChatGroq(model="llama-3.2-90b-vision-preview")
21
+
22
+ print("Creating message...")
23
+ message = HumanMessage(
24
+ content=[
25
+ {
26
+ "type": "text",
27
+ "text": """Analyse the students performance very strictly,
28
+ and generate a detailed 10 point summary informing about the performance and weakness of the student.
29
+ Keep More emphasis on the theoretical marks of the student."""
30
+ },
31
+ {
32
+ "type": "image_url",
33
+ "image_url": {
34
+ "url": f"data:{mime_type};base64,{encoded_file}"
35
+ }
36
+ }
37
+ ]
38
+ )
39
+
40
+ print("Sending request to ChatGroq...")
41
+ response = llm.invoke([message])
42
+ print("Response received!")
43
+
44
+ return response.content
45
+
46
+ def generate_plan(report):
47
+ api_key = os.getenv("GROQ_API_KEY")
48
+ if not api_key:
49
+ raise ValueError("GROQ_API_KEY not found")
50
+
51
+ print("Initializing ChatGroq...")
52
+ llm = ChatGroq(model="llama-3.3-70b-versatile")
53
+
54
+ print("Creating message...")
55
+ message = HumanMessage(
56
+ content=[
57
+ {
58
+ "type": "text",
59
+ "text": f"""Based on the given report Generate a Learning plan for studying the weak subjects.
60
+ Since the student might have school during the weekdays, balance out the workload and study hours accordingly.
61
+ <report>{report}</report>"""
62
+ },
63
+ ]
64
+ )
65
+ print("Sending request to ChatGroq...")
66
+ response = llm.invoke([message])
67
+ print("Response received!")
68
+
69
+ return response.content
70
+
71
+
72
+
73
+ if __name__ == "__main__":
74
+ file = st.file_uploader(label="Upload Latest Report Card:-")
75
+ print(f"Testing with file: {file}")
76
+ col1,col2=st.columns(2)
77
+
78
+ with col1:
79
+ if st.button("Analyse"):
80
+ res=analyse(file)
81
+ st.write(res)
82
+
83
+ with col2:
84
+ if st.button("Generate Plan"):
85
+ aux=analyse(file)
86
+ res=generate_plan(aux)
87
+ st.write(res)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ langchain
2
+ langchain_groq
3
+ groq
4
+ # bs64
5
+ python-dotenv
6
+ streamlit
utils.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import os
3
+
4
+ def encode_image_to_base64(image_path):
5
+ """
6
+ Read an image file and convert it to a base64 string.
7
+
8
+ Args:
9
+ image_path (str): Path to the image file
10
+
11
+ Returns:
12
+ tuple: (base64_string, mime_type)
13
+ """
14
+ try:
15
+ # Get the file extension
16
+ _, ext = os.path.splitext(image_path)
17
+ ext = ext.lower()
18
+
19
+ # Determine MIME type
20
+ mime_types = {
21
+ '.png': 'image/png',
22
+ '.jpg': 'image/jpeg',
23
+ '.jpeg': 'image/jpeg'
24
+ }
25
+
26
+ mime_type = mime_types.get(ext, 'image/png')
27
+
28
+ with open(image_path, 'rb') as image_file:
29
+ # Read the binary data
30
+ binary_data = image_file.read()
31
+ # Encode to base64
32
+ base64_encoded = base64.b64encode(binary_data)
33
+ # Convert to string and remove b'' prefix
34
+ return base64_encoded.decode('utf-8'), mime_type
35
+ except FileNotFoundError:
36
+ raise FileNotFoundError(f"Image file not found at: {image_path}")
37
+ except Exception as e:
38
+ raise Exception(f"Error encoding image: {str(e)}")
39
+
40
+
41
+ # print(encode_image_to_base64("backiee-81547.jpg"))