Spaces:
Runtime error
Runtime error
Federico69
commited on
Commit
•
b404f69
1
Parent(s):
6140c86
Upload folder using huggingface_hub
Browse files- README.md +2 -8
- chatfile.py +37 -0
README.md
CHANGED
@@ -1,12 +1,6 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
|
4 |
-
colorFrom: blue
|
5 |
-
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.28.3
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: chatfile
|
3 |
+
app_file: chatfile.py
|
|
|
|
|
4 |
sdk: gradio
|
5 |
sdk_version: 4.28.3
|
|
|
|
|
6 |
---
|
|
|
|
chatfile.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
|
4 |
+
def answer_question(files, question, api_key):
|
5 |
+
# Utilizar la API key pasada como parámetro
|
6 |
+
openai.api_key = api_key
|
7 |
+
|
8 |
+
# Aquí deberías procesar los archivos PDF para extraer el texto
|
9 |
+
# Simularemos que 'file_content' es el texto extraído
|
10 |
+
file_content = "Texto extraído de tus archivos PDF."
|
11 |
+
|
12 |
+
# Configura y realiza la consulta a la API de OpenAI
|
13 |
+
response = openai.ChatCompletion.create(
|
14 |
+
model="gpt-4",
|
15 |
+
messages=[
|
16 |
+
{"role": "system", "content": "You are a knowledgeable assistant trained to answer questions from uploaded PDF documents."},
|
17 |
+
{"role": "user", "content": question}
|
18 |
+
],
|
19 |
+
tools={"file_search": {"enabled": True}}
|
20 |
+
)
|
21 |
+
|
22 |
+
# Devuelve la respuesta del modelo
|
23 |
+
return response['choices'][0]['message']['content']
|
24 |
+
|
25 |
+
# Configuración de la interfaz de Gradio
|
26 |
+
interface = gr.Interface(
|
27 |
+
fn=answer_question,
|
28 |
+
inputs=[
|
29 |
+
gr.File(file_count="multiple", file_types=["pdf"]),
|
30 |
+
gr.Textbox(label="What is your question?"),
|
31 |
+
gr.Secret(label="API Key") # Campo para ingresar la API key de forma segura
|
32 |
+
],
|
33 |
+
outputs="text"
|
34 |
+
)
|
35 |
+
|
36 |
+
# Ejecuta la aplicación
|
37 |
+
interface.launch()
|