Spaces:
Sleeping
Sleeping
cha0smagick
commited on
Commit
•
24ba3c2
1
Parent(s):
f7f17a3
Update app.py
Browse files
app.py
CHANGED
@@ -3,59 +3,54 @@ from PIL import Image
|
|
3 |
import textwrap
|
4 |
import google.generativeai as genai
|
5 |
|
6 |
-
#
|
7 |
-
def to_markdown(
|
8 |
-
|
9 |
-
return textwrap.indent(
|
10 |
-
|
11 |
-
#
|
12 |
-
def
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
16 |
return None
|
17 |
|
18 |
-
|
19 |
-
return
|
20 |
|
21 |
-
# Streamlit
|
22 |
def main():
|
23 |
-
st.title("
|
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 |
-
#
|
33 |
-
prompt = st.text_area("
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
|
38 |
-
#
|
39 |
-
if
|
40 |
-
st.image(
|
41 |
|
42 |
-
#
|
43 |
-
if st.button("
|
44 |
-
st.markdown("###
|
45 |
-
if not
|
46 |
-
st.warning("
|
47 |
else:
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
#
|
52 |
-
if
|
53 |
-
if
|
54 |
-
|
55 |
-
|
56 |
-
st.markdown(to_markdown(
|
57 |
else:
|
58 |
-
st.warning("No
|
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()
|