CoolFace
Apppublic

Mr-Vicky-01/Artificial-Intelligence-API

sourceHugging Faceupdated 3y agoView on Hugging Face
3likes
app.py40 linesDownload Raw Back to root
1# import required libraries2from dotenv import load_dotenv 3load_dotenv()  # to load all the env variables4 5import gradio as gr6import os7import google.generativeai as genai8from PIL import Image9import numpy as np10 11# Configure api key12genai.configure(api_key= os.getenv("GOOGLE_API_KEY"))13 14# creating function to load gemini pro model15model = genai.GenerativeModel("gemini-pro")16model1 = genai.GenerativeModel("gemini-pro-vision")17 18def get_response(text_input, image_input):19    # Convert NumPy array to PIL Image20    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 or 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.text35    else:36        return "No response available"37      38# Correct the function name in the gr.Interface39demo = gr.Interface(fn=get_response, inputs=['text', gr.Image()], outputs="text", title='Artificial Intelligence API')40demo.launch(debug=True, share=True)