File size: 1,327 Bytes
ca7391d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c395f1c
ca7391d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# import required libraries
from dotenv import load_dotenv 
load_dotenv()  # to load all the env variables

import gradio as gr
import os
import google.generativeai as genai
from PIL import Image
import numpy as np

# Configure api key
genai.configure(api_key= os.getenv("GOOGLE_API_KEY"))

# creating function to load gemini pro model
model = genai.GenerativeModel("gemini-pro")
model1 = genai.GenerativeModel("gemini-pro-vision")

def get_response(text_input, image_input):
    # Convert NumPy array to PIL Image
    if isinstance(image_input, np.ndarray):
        image_input = Image.fromarray(np.uint8(image_input))

    if text_input == '' and image_input is None:
        return "Please provide a text or image."

    if text_input != '' and image_input is not None:
        response = model1.generate_content([text_input, image_input])
    elif image_input is None:
        response = model.generate_content(text_input)
    elif text_input == '':
        response = model1.generate_content(image_input)

    if response is not None:
        return response.text
    else:
        return "No response available"
      
# Correct the function name in the gr.Interface
demo = gr.Interface(fn=get_response, inputs=['text', gr.Image()], outputs="text", title='Artificial Intelligence API')
demo.launch(debug=True, share=True)