nabeelarain713 commited on
Commit
d698715
·
verified ·
1 Parent(s): 2ad8309
Files changed (1) hide show
  1. app.py +26 -37
app.py CHANGED
@@ -1,9 +1,6 @@
1
  import cv2
2
- import mediapipe as mp
3
  import numpy as np
4
  import google.generativeai as genai
5
- from langchain_core.messages import HumanMessage
6
- from langchain_google_genai import ChatGoogleGenerativeAI
7
  import os
8
  import streamlit as st
9
  from PIL import Image, ImageDraw
@@ -11,14 +8,7 @@ from PIL import Image, ImageDraw
11
  # Set up environment variables and configurations
12
  genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
13
 
14
- # Set up MediaPipe (not used in the current implementation but kept for reference)
15
- mp_hands = mp.solutions.hands
16
- mp_drawing = mp.solutions.drawing_utils
17
- hands = mp_hands.Hands()
18
-
19
  # Initialize session state for drawing
20
- if 'drawing' not in st.session_state:
21
- st.session_state.drawing = False
22
  if 'points' not in st.session_state:
23
  st.session_state.points = []
24
 
@@ -36,60 +26,59 @@ def main():
36
 
37
  with col1:
38
  # Create a canvas for drawing
 
 
 
39
  canvas_placeholder = st.empty()
40
- result_placeholder = st.empty()
41
- if 'canvas_image' not in st.session_state:
42
- st.session_state.canvas_image = Image.new("RGB", (800, 600), (0, 0, 0))
43
  if st.button("Reset"):
44
  st.session_state.points = []
45
- st.session_state.canvas_image = Image.new("RGB", (800, 600), (0, 0, 0))
46
  if st.button("Submit"):
47
- drawing_canvas = st.session_state.canvas_image.copy()
48
- draw_on_image(drawing_canvas, st.session_state.points)
49
- drawing_canvas.save('drawing.png')
50
 
51
  # Send the image to Gemini and get the response
52
  response = send_to_gemini('drawing.png')
53
- result_placeholder.text_area("Result:", value=response, height=300)
54
 
55
- canvas_image = np.array(st.session_state.canvas_image)
56
- cv2.putText(canvas_image, "Draw here", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
57
- canvas_placeholder.image(canvas_image)
 
 
58
 
59
- # Handle mouse events for drawing
60
- def update_canvas(image):
61
- if st.session_state.drawing:
62
- current_pos = st.session_state.current_pos
63
- if st.session_state.new_stroke:
64
- st.session_state.points.append([])
65
- st.session_state.new_stroke = False
66
- st.session_state.points[-1].append(current_pos)
67
- draw_on_image(image, st.session_state.points)
68
 
69
- st.session_state.drawing = st.button("Start Drawing", key="start")
70
- st.session_state.current_pos = st.mouse("Mouse Position")
71
- st.session_state.new_stroke = st.button("New Stroke", key="new_stroke")
72
- update_canvas(st.session_state.canvas_image)
73
 
74
  with col2:
75
  st.header("Instructions")
76
  st.write("1. Use the left side to draw your equation.")
77
  st.write("2. Click **Submit** to process the drawing.")
78
  st.write("3. The result will be displayed below after submission.")
 
 
 
79
 
80
- def send_to_gemini(drawing_canvas):
81
  llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash-latest")
 
 
82
  message = HumanMessage(
83
  content=[
84
  {
85
  "type": "text",
86
  "text": "Give me the answer of any mathematical representation in the image with the complete solution, and does not say the image contains etc.",
87
  },
88
- {"type": "image_url", "image_url": drawing_canvas},
89
  ]
90
  )
91
  response = llm.invoke([message]).content
92
  return response
93
 
94
  if __name__ == "__main__":
95
- main()
 
1
  import cv2
 
2
  import numpy as np
3
  import google.generativeai as genai
 
 
4
  import os
5
  import streamlit as st
6
  from PIL import Image, ImageDraw
 
8
  # Set up environment variables and configurations
9
  genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
10
 
 
 
 
 
 
11
  # Initialize session state for drawing
 
 
12
  if 'points' not in st.session_state:
13
  st.session_state.points = []
14
 
 
26
 
27
  with col1:
28
  # Create a canvas for drawing
29
+ canvas_image = Image.new("RGB", (800, 600), (0, 0, 0))
30
+
31
+ # Display the canvas
32
  canvas_placeholder = st.empty()
33
+
34
+ # Instructions and buttons
35
+ st.write("**Draw your equation below:**")
36
  if st.button("Reset"):
37
  st.session_state.points = []
 
38
  if st.button("Submit"):
39
+ draw_on_image(canvas_image, st.session_state.points)
40
+ canvas_image.save('drawing.png')
 
41
 
42
  # Send the image to Gemini and get the response
43
  response = send_to_gemini('drawing.png')
44
+ st.session_state.result = response
45
 
46
+ # Interactive canvas for drawing
47
+ st.write("**Use your mouse to draw:**")
48
+ mouse_down = st.button("Hold to Draw")
49
+ if mouse_down:
50
+ st.session_state.points.append(st.mouse_position())
51
 
52
+ # Draw on the image
53
+ draw_on_image(canvas_image, st.session_state.points)
 
 
 
 
 
 
 
54
 
55
+ # Display the updated image
56
+ canvas_placeholder.image(np.array(canvas_image))
 
 
57
 
58
  with col2:
59
  st.header("Instructions")
60
  st.write("1. Use the left side to draw your equation.")
61
  st.write("2. Click **Submit** to process the drawing.")
62
  st.write("3. The result will be displayed below after submission.")
63
+ if 'result' in st.session_state:
64
+ st.write("**Result:**")
65
+ st.text_area("", value=st.session_state.result, height=300)
66
 
67
+ def send_to_gemini(drawing_path):
68
  llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash-latest")
69
+ with open(drawing_path, 'rb') as img_file:
70
+ image_content = img_file.read()
71
  message = HumanMessage(
72
  content=[
73
  {
74
  "type": "text",
75
  "text": "Give me the answer of any mathematical representation in the image with the complete solution, and does not say the image contains etc.",
76
  },
77
+ {"type": "image", "image": image_content},
78
  ]
79
  )
80
  response = llm.invoke([message]).content
81
  return response
82
 
83
  if __name__ == "__main__":
84
+ main()