Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,27 +2,64 @@
|
|
| 2 |
|
| 3 |
import streamlit as st
|
| 4 |
import os
|
| 5 |
-
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
-
import fitz #
|
| 8 |
-
import base64
|
| 9 |
|
| 10 |
-
# Cargar
|
| 11 |
load_dotenv()
|
| 12 |
-
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import streamlit as st
|
| 4 |
import os
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
+
import fitz # PyMuPDF para leer PDFs
|
|
|
|
| 8 |
|
| 9 |
+
# Cargar variables de entorno
|
| 10 |
load_dotenv()
|
|
|
|
| 11 |
|
| 12 |
+
# Obtener API Key de Gemini
|
| 13 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 14 |
|
| 15 |
+
# Configurar la API de Gemini
|
| 16 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 17 |
|
| 18 |
+
# Modelo de Gemini
|
| 19 |
+
MODEL_NAME = "gemini-1.5-pro"
|
| 20 |
|
| 21 |
+
# Función para generar contenido con Gemini
|
| 22 |
+
def generate_response(user_input):
|
| 23 |
+
try:
|
| 24 |
+
model = genai.GenerativeModel(MODEL_NAME)
|
| 25 |
+
response = model.generate_content(user_input)
|
| 26 |
+
return response.text if response else "No se recibió respuesta."
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error en la generación de contenido: {str(e)}"
|
| 29 |
|
| 30 |
+
# Función para extraer texto de un PDF
|
| 31 |
+
def extract_text_from_pdf(pdf_file):
|
| 32 |
+
try:
|
| 33 |
+
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
| 34 |
+
text = "\n".join([page.get_text() for page in doc])
|
| 35 |
+
return text if text else "No se pudo extraer texto del PDF."
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Error al leer el PDF: {str(e)}"
|
| 38 |
+
|
| 39 |
+
# Interfaz con Streamlit
|
| 40 |
+
st.set_page_config(page_title="Generador con Gemini API", layout="centered")
|
| 41 |
+
st.title("Chat con Gemini API")
|
| 42 |
+
|
| 43 |
+
# Entrada del usuario (texto manual)
|
| 44 |
+
user_input = st.text_area("Escribe algo:", "")
|
| 45 |
+
|
| 46 |
+
# Opción para subir un archivo PDF
|
| 47 |
+
uploaded_file = st.file_uploader("📄 O sube un PDF", type=["pdf"])
|
| 48 |
+
|
| 49 |
+
# Botón para generar respuesta
|
| 50 |
+
if st.button("Generar respuesta"):
|
| 51 |
+
if uploaded_file:
|
| 52 |
+
st.write("⏳ Extrayendo texto del PDF...")
|
| 53 |
+
extracted_text = extract_text_from_pdf(uploaded_file)
|
| 54 |
+
st.text_area("📄 Texto extraído:", extracted_text, height=200)
|
| 55 |
+
user_input = extracted_text # Usar el texto extraído para la consulta
|
| 56 |
+
|
| 57 |
+
if user_input:
|
| 58 |
+
st.write("⏳ Procesando con Gemini...")
|
| 59 |
+
response = generate_response(user_input)
|
| 60 |
+
st.subheader("🔹 Respuesta de Gemini:")
|
| 61 |
+
st.write(response)
|
| 62 |
+
else:
|
| 63 |
+
st.warning("⚠️ Ingresa un texto o sube un PDF antes de continuar.")
|
| 64 |
+
|
| 65 |
+
st.write("💡 Desarrollado por Mariana 🚀")
|