nabeelarain713 commited on
Commit
cc85ee7
·
verified ·
1 Parent(s): 3fa9082
Files changed (1) hide show
  1. app.py +21 -38
app.py CHANGED
@@ -3,57 +3,40 @@ import numpy as np
3
  import google.generativeai as genai
4
  import os
5
  import streamlit as st
6
- from PIL import Image, ImageDraw
 
7
 
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
-
15
- # Function to draw on the canvas
16
- def draw_on_image(image, points):
17
- draw = ImageDraw.Draw(image)
18
- for stroke in points:
19
- if len(stroke) > 1:
20
- draw.line(stroke, fill=(255, 255, 255), width=5)
21
-
22
  def main():
23
  st.title("Virtual Math Calculator")
24
 
25
  col1, col2 = st.columns([3, 1])
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")
 
3
  import google.generativeai as genai
4
  import os
5
  import streamlit as st
6
+ from PIL import Image
7
+ from streamlit_drawable_canvas import st_canvas
8
 
9
  # Set up environment variables and configurations
10
  genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
11
 
 
 
 
 
 
 
 
 
 
 
 
12
  def main():
13
  st.title("Virtual Math Calculator")
14
 
15
  col1, col2 = st.columns([3, 1])
16
 
17
  with col1:
18
+ # Create a drawing canvas
 
 
 
 
 
 
19
  st.write("**Draw your equation below:**")
20
+ canvas_result = st_canvas(
21
+ fill_color="rgb(255, 255, 255)", # Fill color for drawing
22
+ stroke_width=5,
23
+ stroke_color="rgb(0, 0, 0)",
24
+ background_color="rgb(255, 255, 255)",
25
+ height=300,
26
+ width=600,
27
+ drawing_mode="freedraw",
28
+ key="canvas",
29
+ )
30
 
31
+ if st.button("Submit"):
32
+ if canvas_result.image_data is not None:
33
+ # Convert to image
34
+ drawing_image = Image.fromarray(canvas_result.image_data.astype('uint8'), 'RGBA').convert('RGB')
35
+ drawing_image.save('drawing.png')
 
 
 
36
 
37
+ # Send the image to Gemini and get the response
38
+ response = send_to_gemini('drawing.png')
39
+ st.session_state.result = response
40
 
41
  with col2:
42
  st.header("Instructions")