Spaces:
Sleeping
Sleeping
Brahmadev619
commited on
Commit
•
1eaf351
1
Parent(s):
ac4638d
Upload 2 files
Browse files- app.py +78 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import PIL.Image
|
2 |
+
import gradio as gr
|
3 |
+
import base64
|
4 |
+
import time
|
5 |
+
import os
|
6 |
+
import google.generativeai as genai
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
# Set Google API key
|
13 |
+
genai.configure(api_key=os.getenv("'GOOGLE_API_KEY'"))
|
14 |
+
|
15 |
+
# os.environ['GOOGLE_API_KEY'] = ""
|
16 |
+
# genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
|
17 |
+
|
18 |
+
# Create the Model
|
19 |
+
txt_model = genai.GenerativeModel('gemini-pro')
|
20 |
+
vis_model = genai.GenerativeModel('gemini-pro-vision')
|
21 |
+
|
22 |
+
|
23 |
+
# Image to Base 64 Converter
|
24 |
+
def image_to_base64(image_path):
|
25 |
+
with open(image_path, 'rb') as img:
|
26 |
+
encoded_string = base64.b64encode(img.read())
|
27 |
+
return encoded_string.decode('utf-8')
|
28 |
+
|
29 |
+
|
30 |
+
# Function that takes User Inputs and displays it on ChatUI
|
31 |
+
def query_message(history, txt, img):
|
32 |
+
if not img:
|
33 |
+
history.append((txt, None))
|
34 |
+
return history
|
35 |
+
base64_data = image_to_base64(img)
|
36 |
+
data_url = f"data:image/jpeg;base64,{base64_data}"
|
37 |
+
history.append((f"{txt} ![]({data_url})", None))
|
38 |
+
return history
|
39 |
+
|
40 |
+
|
41 |
+
# Function that takes User Inputs, generates Response and displays on Chat UI
|
42 |
+
def llm_response(history, text, img):
|
43 |
+
if not img:
|
44 |
+
response = txt_model.generate_content(text)
|
45 |
+
history.append((None, response.text))
|
46 |
+
return history
|
47 |
+
|
48 |
+
else:
|
49 |
+
img = PIL.Image.open(img)
|
50 |
+
response = vis_model.generate_content([text, img])
|
51 |
+
history.append((None, response.text))
|
52 |
+
return history
|
53 |
+
|
54 |
+
|
55 |
+
# Interface Code
|
56 |
+
with gr.Blocks() as app:
|
57 |
+
with gr.Row():
|
58 |
+
image_box = gr.Image(type="filepath")
|
59 |
+
|
60 |
+
chatbot = gr.Chatbot(
|
61 |
+
scale=2,
|
62 |
+
height=600
|
63 |
+
)
|
64 |
+
text_box = gr.Textbox(
|
65 |
+
placeholder="Enter text and press enter, or upload an image",
|
66 |
+
container=False,
|
67 |
+
)
|
68 |
+
|
69 |
+
btn = gr.Button("Submit")
|
70 |
+
clicked = btn.click(query_message,
|
71 |
+
[chatbot, text_box, image_box],
|
72 |
+
chatbot
|
73 |
+
).then(llm_response,
|
74 |
+
[chatbot, text_box, image_box],
|
75 |
+
chatbot
|
76 |
+
)
|
77 |
+
app.queue()
|
78 |
+
app.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
google-generativeai
|
2 |
+
gradio
|
3 |
+
Pillow
|