Upload 3 files
Browse files- .env +1 -0
- app.py +40 -0
- requirements.txt +5 -0
.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
GOOGLE_API_KEY='AIzaSyD1O5UaebHbAan9cCNomNP3hGXloC5-FT8'
|
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import required libraries
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
load_dotenv() # to load all the env variables
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import os
|
| 7 |
+
import google.generativeai as genai
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
# Configure api key
|
| 12 |
+
genai.configure(api_key= os.getenv("GOOGLE_API_KEY"))
|
| 13 |
+
|
| 14 |
+
# creating function to load gemini pro model
|
| 15 |
+
model = genai.GenerativeModel("gemini-pro")
|
| 16 |
+
model1 = genai.GenerativeModel("gemini-pro-vision")
|
| 17 |
+
|
| 18 |
+
def get_response(text_input, image_input):
|
| 19 |
+
# Convert NumPy array to PIL Image
|
| 20 |
+
if isinstance(image_input, np.ndarray):
|
| 21 |
+
image_input = Image.fromarray(np.uint8(image_input))
|
| 22 |
+
|
| 23 |
+
if text_input == '' and image_input is None:
|
| 24 |
+
return "Please provide a text and an image."
|
| 25 |
+
|
| 26 |
+
if text_input != '' and image_input is not None:
|
| 27 |
+
response = model1.generate_content([text_input, image_input])
|
| 28 |
+
elif image_input is None:
|
| 29 |
+
response = model.generate_content(text_input)
|
| 30 |
+
elif text_input == '':
|
| 31 |
+
response = model1.generate_content(image_input)
|
| 32 |
+
|
| 33 |
+
if response is not None:
|
| 34 |
+
return response.text
|
| 35 |
+
else:
|
| 36 |
+
return "No response available"
|
| 37 |
+
|
| 38 |
+
# Correct the function name in the gr.Interface
|
| 39 |
+
demo = gr.Interface(fn=get_response, inputs=['text', gr.Image()], outputs="text", title='Artificial Intelligence API')
|
| 40 |
+
demo.launch(debug=True, share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
numpy
|
| 3 |
+
google-generativeai
|
| 4 |
+
python-dotenv
|
| 5 |
+
Pillow
|