Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ GEMINI_API_KEY = "AIzaSyCZwFUTPjCIx5reXELxu8Tby2wwu-s_K9o"
|
|
| 10 |
def generate_response(user_input):
|
| 11 |
try:
|
| 12 |
client = genai.Client(api_key=GEMINI_API_KEY)
|
|
|
|
| 13 |
model = "gemini-2.0-flash-thinking-exp-01-21"
|
| 14 |
contents = [
|
| 15 |
types.Content(
|
|
@@ -49,43 +50,30 @@ def extract_text_from_pdf(pdf_file):
|
|
| 49 |
except Exception as e:
|
| 50 |
return f"Se produjo un error al leer el PDF: {e}"
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
if "chat_history" not in st.session_state:
|
| 54 |
-
st.session_state.chat_history = []
|
| 55 |
-
|
| 56 |
st.title("Gemini LLM App")
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
option = st.
|
| 60 |
|
| 61 |
-
# Opci贸n 1:
|
| 62 |
-
if option == "
|
| 63 |
-
|
| 64 |
user_input = st.text_area("Introduce tu solicitud:", placeholder="Escribe algo aqu铆...")
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
if user_input.strip():
|
| 68 |
with st.spinner("Generando respuesta..."):
|
| 69 |
response = generate_response(user_input)
|
| 70 |
-
# Agregar al historial
|
| 71 |
-
st.session_state.chat_history.append({
|
| 72 |
-
"usuario": user_input,
|
| 73 |
-
"respuesta": response
|
| 74 |
-
})
|
| 75 |
st.success("隆Respuesta generada!")
|
|
|
|
| 76 |
else:
|
| 77 |
st.error("Por favor, introduce una solicitud para continuar.")
|
| 78 |
|
| 79 |
-
# Mostrar historial de conversaciones
|
| 80 |
-
st.subheader("Historial de Conversaciones")
|
| 81 |
-
for idx, msg in enumerate(st.session_state.chat_history):
|
| 82 |
-
st.markdown(f"**Usuario {idx+1}:** {msg['usuario']}")
|
| 83 |
-
st.markdown(f"**Gemini:** {msg['respuesta']}")
|
| 84 |
-
st.markdown("---")
|
| 85 |
-
|
| 86 |
# Opci贸n 2: Subir y preguntar sobre un PDF
|
| 87 |
-
elif option == "PDF":
|
| 88 |
-
|
| 89 |
uploaded_file = st.file_uploader("Sube un archivo PDF", type="pdf")
|
| 90 |
|
| 91 |
if uploaded_file is not None:
|
|
@@ -93,19 +81,19 @@ elif option == "PDF":
|
|
| 93 |
pdf_text = extract_text_from_pdf(uploaded_file)
|
| 94 |
|
| 95 |
if pdf_text:
|
| 96 |
-
st.success("Texto extra铆do correctamente.")
|
|
|
|
| 97 |
# Mostrar una parte del contenido extra铆do
|
| 98 |
st.text_area("Contenido del PDF:", pdf_text[:1000], height=200)
|
| 99 |
-
|
| 100 |
# Entrada de pregunta del usuario sobre el PDF
|
| 101 |
user_question = st.text_area("Haz una pregunta sobre el contenido del PDF:", placeholder="Escribe tu pregunta aqu铆...")
|
| 102 |
-
|
|
|
|
| 103 |
if st.button("Obtener Respuesta"):
|
| 104 |
if user_question.strip():
|
| 105 |
with st.spinner("Generando respuesta..."):
|
| 106 |
-
|
| 107 |
-
combined_input = pdf_text + "\nPregunta: " + user_question
|
| 108 |
-
response = generate_response(combined_input)
|
| 109 |
st.success("隆Respuesta generada!")
|
| 110 |
st.write(response)
|
| 111 |
else:
|
|
@@ -113,4 +101,4 @@ elif option == "PDF":
|
|
| 113 |
else:
|
| 114 |
st.error("No se pudo extraer texto del archivo PDF. Por favor, intenta con otro archivo.")
|
| 115 |
else:
|
| 116 |
-
st.info("Por favor, sube un archivo PDF para empezar.")
|
|
|
|
| 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(
|
|
|
|
| 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:
|
|
|
|
| 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:
|
|
|
|
| 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.")
|