File size: 2,432 Bytes
2ccc12d
 
 
d69e4c3
 
2ccc12d
 
cc85ee7
 
2ccc12d
 
d69e4c3
2ccc12d
2ad8309
 
2ccc12d
2ad8309
 
 
cc85ee7
d698715
cc85ee7
 
 
 
 
 
 
 
 
 
2ad8309
cc85ee7
 
 
 
 
 
 
 
 
2ad8309
 
 
a9179f9
2ad8309
 
d698715
 
 
2ccc12d
d698715
2ccc12d
16aea0b
 
2ccc12d
d69e4c3
 
 
 
 
e8c020b
d69e4c3
2ccc12d
 
 
 
 
d698715
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import cv2
import numpy as np
import google.generativeai as genai
from langchain_core.messages import HumanMessage
from langchain_google_genai import ChatGoogleGenerativeAI
import os
import streamlit as st
from PIL import Image
from streamlit_drawable_canvas import st_canvas

# Set up environment variables and configurations
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])

def main():
    st.title("Virtual Math Calculator")

    col1, col2 = st.columns([3, 1])

    with col1:
        # Create a drawing canvas
        st.write("**Draw your equation below:**")
        canvas_result = st_canvas(
            fill_color="rgb(255, 255, 255)",  # Fill color for drawing
            stroke_width=5,
            stroke_color="rgb(0, 0, 0)",
            background_color="rgb(255, 255, 255)",
            height=300,
            width=600,
            drawing_mode="freedraw",
            key="canvas",
        )

        if st.button("Submit"):
            if canvas_result.image_data is not None:
                # Convert to image
                drawing_image = Image.fromarray(canvas_result.image_data.astype('uint8'), 'RGBA').convert('RGB')
                drawing_image.save('drawing.png')

                # Send the image to Gemini and get the response
                response = send_to_gemini('drawing.png')
                st.session_state.result = response

    with col2:
        st.header("Instructions")
        st.write("1. Use the mouse pointer/finger to draw your equation.")
        st.write("2. Click **Submit** to process the drawing.")
        st.write("3. The result will be displayed below after submission.")
        if 'result' in st.session_state:
            st.write("**Result:**")
            st.text_area("", value=st.session_state.result, height=300)

def send_to_gemini(drawing_path):
    llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash-latest")
    # with open(drawing_path, 'rb') as img_file:
    #     image_content = img_file.read()
    message = HumanMessage(
        content=[
            {
                "type": "text",
                "text": "Give me the answer of any mathematical representation in the image with the complete solution, and does not say the image contains etc.",
            },
            {"type": "image_url", "image_url": drawing_path},
        ]
    )
    response = llm.invoke([message]).content
    return response

if __name__ == "__main__":
    main()