Spaces:
Runtime error
Runtime error
niladridutta
commited on
Commit
•
cddb102
1
Parent(s):
a63107b
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import streamlit as st
|
3 |
+
import os
|
4 |
+
import pathlib
|
5 |
+
import textwrap
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
|
9 |
+
import google.generativeai as genai
|
10 |
+
|
11 |
+
genai.configure(api_key='AIzaSyAA_R5VXv1qjJ5jDMObkluREA8BxJO67RU')
|
12 |
+
|
13 |
+
## Function to load OpenAI model and get respones
|
14 |
+
|
15 |
+
def get_gemini_response(input,image,prompt):
|
16 |
+
model = genai.GenerativeModel('gemini-pro-vision')
|
17 |
+
response = model.generate_content([input,image[0],prompt])
|
18 |
+
return response.text
|
19 |
+
|
20 |
+
|
21 |
+
def input_image_setup(uploaded_file):
|
22 |
+
# Check if a file has been uploaded
|
23 |
+
if uploaded_file is not None:
|
24 |
+
# Read the file into bytes
|
25 |
+
bytes_data = uploaded_file.getvalue()
|
26 |
+
|
27 |
+
image_parts = [
|
28 |
+
{
|
29 |
+
"mime_type": uploaded_file.type, # Get the mime type of the uploaded file
|
30 |
+
"data": bytes_data
|
31 |
+
}
|
32 |
+
]
|
33 |
+
return image_parts
|
34 |
+
else:
|
35 |
+
raise FileNotFoundError("No file uploaded")
|
36 |
+
|
37 |
+
|
38 |
+
##initialize our streamlit app
|
39 |
+
|
40 |
+
st.set_page_config(page_title="Gemini Image Demo")
|
41 |
+
|
42 |
+
st.header("Generative AI : Invoice Reader")
|
43 |
+
input=st.text_input("Input Prompt: ",key="input")
|
44 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
45 |
+
image=""
|
46 |
+
if uploaded_file is not None:
|
47 |
+
image = Image.open(uploaded_file)
|
48 |
+
st.image(image, caption="Uploaded Image.", use_column_width=True)
|
49 |
+
|
50 |
+
|
51 |
+
submit=st.button("Tell me about the image")
|
52 |
+
|
53 |
+
input_prompt = """
|
54 |
+
You are an expert in pharmaceutical field, reading medicine names.
|
55 |
+
You will receive input images as prescription &
|
56 |
+
you will have to detect medicine names as correctly
|
57 |
+
from complicated and poor english handwriting, as done by an OCR model
|
58 |
+
"""
|
59 |
+
|
60 |
+
## If ask button is clicked
|
61 |
+
|
62 |
+
if submit:
|
63 |
+
image_data = input_image_setup(uploaded_file)
|
64 |
+
response=get_gemini_response(input_prompt,image_data,input)
|
65 |
+
st.subheader("The Response is")
|
66 |
+
st.write(response)
|