File size: 2,152 Bytes
56f6374
5b568c0
8fcc99d
 
5b568c0
8fcc99d
 
 
 
5b568c0
8fcc99d
8ed9429
8fcc99d
8ed9429
1e2e8cd
 
 
8ed9429
8fcc99d
5b568c0
8fcc99d
5b568c0
8fcc99d
 
 
 
 
 
8ed9429
 
8fcc99d
 
 
 
 
 
 
 
 
 
 
 
 
 
8ed9429
 
8fcc99d
8ed9429
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
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-vision', image=None):
    model = genai.GenerativeModel(model_name)
    if not image:
        st.warning("Please add an image to use the gemini-pro-vision model.")
        return None

    response = model.generate_content([prompt, image])
    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)

    # Set the model to 'gemini-pro-vision'
    model_name = '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 not image_file:
            st.warning("Please provide an image for the gemini-pro-vision model.")
        else:
            image = Image.open(image_file)
            response = generate_gemini_content(prompt, model_name=model_name, image=image)

            # Display the generated content in Markdown format if response is available
            if response:
                if response.candidates:
                    parts = response.candidates[0].content.parts
                    generated_text = parts[0].text if parts else "No content generated."
                    st.markdown(to_markdown(generated_text))
                else:
                    st.warning("No candidates found in the response.")

if __name__ == "__main__":
    main()