-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
62 lines (50 loc) · 1.9 KB
/
app.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#Gemini flash model
from dotenv import load_dotenv
load_dotenv() #take environment variables from .env.
import streamlit as st
import os
from PIL import Image
import google.generativeai as genai
#Configure the Gemini API with environment variable
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
#Function to load Gemini model and get response.
def get_gemini_response(input_text, image=None):
model = genai.GenerativeModel("gemini-1.5-flash")
if input_text and image:
response = model.generate_content([input_text, image])
elif input_text:
response = model.generate_content([input_text])
else:
response = model.generate_content([image])
return response.text
#Initialize Streamlit app
st.set_page_config(page_title="generatetext")
#Create two tabs
tab1, tab2 = st.tabs(["Text", "Text & Image"])
#Tab 1: text
with tab1:
st.header("Gemini AI")
input_text = st.text_input("Ask something:", key="text_input")
if st.button("Generate", key="text_response"):
if input_text:
response = get_gemini_response(input_text)
st.subheader("Response:")
st.write(response)
else:
st.warning("Please enter some text!")
#Tab 2: Text and Image
with tab2:
st.header("Gemini AI")
input_text = st.text_input("Ask something:", key="text_and_image_input")
uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
image = None
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image.", use_column_width=True)
if st.button("Generate", key="image_text_response"):
if input_text or image:
response = get_gemini_response(input_text, image)
st.subheader("Response:")
st.write(response)
else:
st.warning("Please provide either text, an image, or both!")