Spaces:
Sleeping
Sleeping
# Import libraries | |
from dotenv import load_dotenv | |
import os | |
import google.generativeai as genai | |
from PIL import Image | |
import streamlit as st | |
# Load API Key | |
load_dotenv() | |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) | |
# Function to load Google Gemini Vision model and get response | |
def get_response_code(image, prompt): | |
model = genai.GenerativeModel('gemini-pro-vision') | |
response = model.generate_content([image[0], prompt]) | |
return response.text | |
# Function to preprocess image data | |
def prep_image(uploaded_file): | |
# Check if there is any data | |
if uploaded_file is not None: | |
# Read the file as bytes | |
bytes_data = uploaded_file.getvalue() | |
# Get the image part information | |
image_parts = [ | |
{ | |
"mime_type": uploaded_file.type, | |
"data": bytes_data | |
} | |
] | |
return image_parts | |
else: | |
raise FileNotFoundError("No File is uploaded!") | |
# Configuring Streamlit App | |
st.set_page_config(page_title="Code Interpreter") | |
st.image('LOGO.jpg', width=70) | |
st.header("Code Interpreter") | |
# Section for Code Interpreter | |
upload_code_file = st.file_uploader("Choose an image of code...", type=["jpg", "jpeg", "png"]) | |
if upload_code_file is not None: | |
# Show the uploaded code image | |
code_image = Image.open(upload_code_file) | |
st.image(code_image, caption="Uploaded Code Image", use_column_width=True) | |
# Prompt Template for code interpretation | |
input_prompt_code = """ | |
You are an expert in coding. The image contains a piece of code. Your task is to interpret the code and solve the problem it presents. | |
Provide a complete solution of the code. | |
""" | |
# Button for code interpretation | |
submit = st.button("Interpret Code!") | |
if submit: | |
code_image_data = prep_image(upload_code_file) | |
response = get_response_code(code_image_data, input_prompt_code) | |
st.subheader("Code AI: ") | |
st.write(response) | |
else: | |
st.info("Please upload an image of the code to proceed.") | |