File size: 1,853 Bytes
56f6374
5b568c0
8fcc99d
 
5b568c0
8fcc99d
 
 
 
5b568c0
8fcc99d
 
 
 
 
 
 
5b568c0
8fcc99d
5b568c0
8fcc99d
5b568c0
8fcc99d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b568c0
 
 
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
import streamlit as st
from PIL import Image
import textwrap
import google.generativeai as genai

# Function to display formatted Markdown text
def to_markdown(text):
    text = text.replace('•', '  *')
    return textwrap.indent(text, '> ', predicate=lambda _: True)

# Function to generate content using Gemini API
def generate_gemini_content(prompt, model_name='gemini-pro', image=None):
    model = genai.GenerativeModel(model_name)
    if image:
        response = model.generate_content([prompt, image])
    else:
        response = model.generate_content(prompt)

    return response

# Streamlit app
def main():
    st.title("Gemini API Demo with Streamlit")

    # Get Gemini API key from user input
    api_key = st.text_input("Enter your Gemini API key:")
    genai.configure(api_key=api_key)

    # Choose a model
    model_name = st.selectbox("Select a Gemini model", ["gemini-pro", "gemini-pro-vision"])

    # Get user input prompt
    prompt = st.text_area("Enter your prompt:")

    # Get optional image input
    image_file = st.file_uploader("Upload an image (if applicable):", type=["jpg", "jpeg", "png"])

    # Display image if provided
    if image_file:
        st.image(image_file, caption="Uploaded Image", use_column_width=True)

    # Generate content on button click
    if st.button("Generate Content"):
        st.markdown("### Generated Content:")
        if image_file:
            # If an image is provided, use gemini-pro-vision model
            image = Image.open(image_file)
            response = generate_gemini_content(prompt, model_name='gemini-pro-vision', image=image)
        else:
            response = generate_gemini_content(prompt, model_name=model_name)

        # Display the generated content in Markdown format
        st.markdown(to_markdown(response.text))

if __name__ == "__main__":
    main()