Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from google import genai
|
| 3 |
+
from google.genai import types
|
| 4 |
+
from PyPDF2 import PdfReader
|
| 5 |
+
|
| 6 |
+
# Configura tu API Key directamente en el c贸digo
|
| 7 |
+
GEMINI_API_KEY = "AIzaSyCZwFUTPjCIx5reXELxu8Tby2wwu-s_K9o"
|
| 8 |
+
|
| 9 |
+
# Funci贸n para generar respuesta desde la API de Google Gemini
|
| 10 |
+
def generate_response(user_input):
|
| 11 |
+
try:
|
| 12 |
+
client = genai.Client(api_key=GEMINI_API_KEY)
|
| 13 |
+
|
| 14 |
+
model = "gemini-2.0-flash-thinking-exp-01-21"
|
| 15 |
+
contents = [
|
| 16 |
+
types.Content(
|
| 17 |
+
role="user",
|
| 18 |
+
parts=[
|
| 19 |
+
types.Part.from_text(text=user_input),
|
| 20 |
+
],
|
| 21 |
+
),
|
| 22 |
+
]
|
| 23 |
+
generate_content_config = types.GenerateContentConfig(
|
| 24 |
+
temperature=0.7,
|
| 25 |
+
top_p=0.95,
|
| 26 |
+
top_k=64,
|
| 27 |
+
max_output_tokens=65536,
|
| 28 |
+
response_mime_type="text/plain",
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
response_text = ""
|
| 32 |
+
for chunk in client.models.generate_content_stream(
|
| 33 |
+
model=model,
|
| 34 |
+
contents=contents,
|
| 35 |
+
config=generate_content_config,
|
| 36 |
+
):
|
| 37 |
+
response_text += chunk.text
|
| 38 |
+
return response_text
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"Se produjo un error: {e}"
|
| 41 |
+
|
| 42 |
+
# Funci贸n para extraer texto de un archivo PDF
|
| 43 |
+
def extract_text_from_pdf(pdf_file):
|
| 44 |
+
try:
|
| 45 |
+
pdf_reader = PdfReader(pdf_file)
|
| 46 |
+
text = ""
|
| 47 |
+
for page in pdf_reader.pages:
|
| 48 |
+
text += page.extract_text() + "\n"
|
| 49 |
+
return text
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"Se produjo un error al leer el PDF: {e}"
|
| 52 |
+
|
| 53 |
+
# Aplicaci贸n Streamlit
|
| 54 |
+
st.title("Gemini LLM App")
|
| 55 |
+
|
| 56 |
+
# Selecci贸n entre Chat o PDF
|
| 57 |
+
option = st.radio("Elige una opci贸n:", ("Hablar con el chat", "Subir y preguntar sobre un PDF"))
|
| 58 |
+
|
| 59 |
+
# Opci贸n 1: Hablar con el chat
|
| 60 |
+
if option == "Hablar con el chat":
|
| 61 |
+
# Entrada del usuario para el chat
|
| 62 |
+
user_input = st.text_area("Introduce tu solicitud:", placeholder="Escribe algo aqu铆...")
|
| 63 |
+
|
| 64 |
+
# Bot贸n para enviar la solicitud
|
| 65 |
+
if st.button("Obtener Respuesta"):
|
| 66 |
+
if user_input.strip():
|
| 67 |
+
with st.spinner("Generando respuesta..."):
|
| 68 |
+
response = generate_response(user_input)
|
| 69 |
+
st.success("隆Respuesta generada!")
|
| 70 |
+
st.write(response)
|
| 71 |
+
else:
|
| 72 |
+
st.error("Por favor, introduce una solicitud para continuar.")
|
| 73 |
+
|
| 74 |
+
# Opci贸n 2: Subir y preguntar sobre un PDF
|
| 75 |
+
elif option == "Subir y preguntar sobre un PDF":
|
| 76 |
+
# Subir archivo PDF
|
| 77 |
+
uploaded_file = st.file_uploader("Sube un archivo PDF", type="pdf")
|
| 78 |
+
|
| 79 |
+
if uploaded_file is not None:
|
| 80 |
+
with st.spinner("Extrayendo texto del PDF..."):
|
| 81 |
+
pdf_text = extract_text_from_pdf(uploaded_file)
|
| 82 |
+
|
| 83 |
+
if pdf_text:
|
| 84 |
+
st.success("Texto extra铆do del PDF correctamente.")
|
| 85 |
+
|
| 86 |
+
# Mostrar una parte del contenido extra铆do
|
| 87 |
+
st.text_area("Contenido del PDF:", pdf_text[:1000], height=200)
|
| 88 |
+
|
| 89 |
+
# Entrada de pregunta del usuario sobre el PDF
|
| 90 |
+
user_question = st.text_area("Haz una pregunta sobre el contenido del PDF:", placeholder="Escribe tu pregunta aqu铆...")
|
| 91 |
+
|
| 92 |
+
# Bot贸n para enviar la pregunta
|
| 93 |
+
if st.button("Obtener Respuesta"):
|
| 94 |
+
if user_question.strip():
|
| 95 |
+
with st.spinner("Generando respuesta..."):
|
| 96 |
+
response = generate_response(pdf_text, user_question)
|
| 97 |
+
st.success("隆Respuesta generada!")
|
| 98 |
+
st.write(response)
|
| 99 |
+
else:
|
| 100 |
+
st.error("Por favor, introduce una pregunta para continuar.")
|
| 101 |
+
else:
|
| 102 |
+
st.error("No se pudo extraer texto del archivo PDF. Por favor, intenta con otro archivo.")
|
| 103 |
+
else:
|
| 104 |
+
st.info("Por favor, sube un archivo PDF para empezar.")
|