cha0smagick commited on
Commit
24ba3c2
1 Parent(s): f7f17a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -43
app.py CHANGED
@@ -3,59 +3,54 @@ from PIL import Image
3
  import textwrap
4
  import google.generativeai as genai
5
 
6
- # Function to display formatted Markdown text
7
- def to_markdown(text):
8
- text = text.replace('•', ' *')
9
- return textwrap.indent(text, '> ', predicate=lambda _: True)
10
-
11
- # Function to generate content using Gemini API
12
- def generate_gemini_content(prompt, model_name='gemini-pro-vision', image=None):
13
- model = genai.GenerativeModel(model_name)
14
- if not image:
15
- st.warning("Please add an image to use the gemini-pro-vision model.")
 
 
16
  return None
17
 
18
- response = model.generate_content([prompt, image])
19
- return response
20
 
21
- # Streamlit app
22
  def main():
23
- st.title("Gemini API Demo with Streamlit")
24
-
25
- # Get Gemini API key from user input
26
- api_key = st.text_input("Enter your Gemini API key:")
27
- genai.configure(api_key=api_key)
28
-
29
- # Set the model to 'gemini-pro-vision'
30
- model_name = 'gemini-pro-vision'
31
 
32
- # Get user input prompt
33
- prompt = st.text_area("Enter your prompt:")
34
 
35
- # Get optional image input
36
- image_file = st.file_uploader("Upload an image (if applicable):", type=["jpg", "jpeg", "png"])
37
 
38
- # Display image if provided
39
- if image_file:
40
- st.image(image_file, caption="Uploaded Image", use_column_width=True)
41
 
42
- # Generate content on button click
43
- if st.button("Generate Content"):
44
- st.markdown("### Generated Content:")
45
- if not image_file:
46
- st.warning("Please provide an image for the gemini-pro-vision model.")
47
  else:
48
- image = Image.open(image_file)
49
- response = generate_gemini_content(prompt, model_name=model_name, image=image)
50
-
51
- # Display the generated content in Markdown format if response is available
52
- if response:
53
- if response.candidates:
54
- parts = response.candidates[0].content.parts
55
- generated_text = parts[0].text if parts else "No content generated."
56
- st.markdown(to_markdown(generated_text))
57
  else:
58
- st.warning("No candidates found in the response.")
59
 
60
  if __name__ == "__main__":
61
  main()
 
3
  import textwrap
4
  import google.generativeai as genai
5
 
6
+ # Función para mostrar texto formateado en Markdown
7
+ def to_markdown(texto):
8
+ texto = texto.replace('•', ' *')
9
+ return textwrap.indent(texto, '> ', predicate=lambda _: True)
10
+
11
+ # Función para generar contenido usando la API Gemini
12
+ def generar_contenido_gemini(prompt, model_name='gemini-pro-vision', imagen=None):
13
+ api_key = "AIzaSyCv30EVBXynJjSNZiYkKqc5xx1DovQEaQY" # Inserta tu API key aquí
14
+ genai.configure(api_key=api_key)
15
+ modelo = genai.GenerativeModel(model_name)
16
+ if not imagen:
17
+ st.warning("Por favor, agrega una imagen para usar el modelo gemini-pro-vision.")
18
  return None
19
 
20
+ respuesta = modelo.generate_content([prompt, imagen])
21
+ return respuesta
22
 
23
+ # App de Streamlit
24
  def main():
25
+ st.title("Demo de la API Gemini con Streamlit")
 
 
 
 
 
 
 
26
 
27
+ # Obtener entrada de texto del usuario
28
+ prompt = st.text_area("Ingresa tu indicación:")
29
 
30
+ # Obtener entrada opcional de imagen
31
+ imagen_archivo = st.file_uploader("Subir una imagen (si aplica):", type=["jpg", "jpeg", "png"])
32
 
33
+ # Mostrar imagen si se proporciona
34
+ if imagen_archivo:
35
+ st.image(imagen_archivo, caption="Imagen Subida", use_column_width=True)
36
 
37
+ # Generar contenido al hacer clic en el botón
38
+ if st.button("Generar Contenido"):
39
+ st.markdown("### Contenido Generado:")
40
+ if not imagen_archivo:
41
+ st.warning("Por favor, proporciona una imagen para el modelo gemini-pro-vision.")
42
  else:
43
+ imagen = Image.open(imagen_archivo)
44
+ respuesta = generar_contenido_gemini(prompt, imagen=imagen)
45
+
46
+ # Mostrar el contenido generado en formato Markdown si hay respuesta disponible
47
+ if respuesta:
48
+ if respuesta.candidates:
49
+ partes = respuesta.candidates[0].content.parts
50
+ texto_generado = partes[0].text if partes else "No se generó contenido."
51
+ st.markdown(to_markdown(texto_generado))
52
  else:
53
+ st.warning("No se encontraron candidatos en la respuesta.")
54
 
55
  if __name__ == "__main__":
56
  main()