Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
|
| 4 |
+
# Crear un modelo generativo
|
| 5 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 6 |
+
|
| 7 |
+
# Funci贸n para procesar el PDF y generar una respuesta
|
| 8 |
+
def process_pdf(file):
|
| 9 |
+
# Subir el archivo PDF local
|
| 10 |
+
sample_pdf = genai.upload_file(file.name)
|
| 11 |
+
|
| 12 |
+
# Crear el prompt para la API
|
| 13 |
+
prompt = "Give me a summary of this pdf file."
|
| 14 |
|
| 15 |
+
# Generar contenido a partir del PDF subido
|
| 16 |
+
response = model.generate_content([prompt, sample_pdf])
|
| 17 |
+
|
| 18 |
+
return response.text
|
| 19 |
+
|
| 20 |
+
# Definir la interfaz de usuario en Gradio
|
| 21 |
+
def chatbot_interface(user_input, history):
|
| 22 |
+
# Procesar el archivo PDF
|
| 23 |
+
response_text = process_pdf(user_input)
|
| 24 |
+
|
| 25 |
+
# Actualizar el historial del chatbot
|
| 26 |
+
history.append((user_input.name, response_text))
|
| 27 |
+
|
| 28 |
+
return history, history
|
| 29 |
+
|
| 30 |
+
# Crear la app de Gradio
|
| 31 |
+
app = gr.Interface(
|
| 32 |
+
fn=chatbot_interface,
|
| 33 |
+
inputs=[
|
| 34 |
+
gr.File(file_count="single", type="file", label="Upload PDF Document"),
|
| 35 |
+
gr.State(), # Mantener el estado del historial
|
| 36 |
+
],
|
| 37 |
+
outputs=[
|
| 38 |
+
gr.Chatbot(),
|
| 39 |
+
gr.State(),
|
| 40 |
+
],
|
| 41 |
+
live=True
|
| 42 |
+
)
|
| 43 |
|
| 44 |
+
# Lanzar la app
|
| 45 |
+
app.launch()
|