Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from flask import Flask, request, jsonify, render_template
|
| 3 |
+
import PyPDF2
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
# Lee la clave de API desde una variable de entorno
|
| 9 |
+
client = OpenAI(
|
| 10 |
+
api_key=os.environ.get("OPENAI_API_KEY"),
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def extract_text_from_file(file):
|
| 14 |
+
# ... (el resto de tu c贸digo de la funci贸n extract_text_from_file)
|
| 15 |
+
if file.filename.endswith('.pdf'):
|
| 16 |
+
pdf_reader = PyPDF2.PdfReader(file)
|
| 17 |
+
text = ""
|
| 18 |
+
for page in pdf_reader.pages:
|
| 19 |
+
text += page.extract_text()
|
| 20 |
+
return text
|
| 21 |
+
|
| 22 |
+
elif file.filename.endswith('.txt'):
|
| 23 |
+
return file.read().decode('utf-8')
|
| 24 |
+
return ""
|
| 25 |
+
|
| 26 |
+
def generate_summary_openai(text):
|
| 27 |
+
# ... (el resto de tu c贸digo de la funci贸n generate_summary_openai)
|
| 28 |
+
try:
|
| 29 |
+
prompt_text = (
|
| 30 |
+
"Eres un asistente experto en resumir documentos. "
|
| 31 |
+
"Genera un resumen del siguiente texto en 5 puntos clave, utilizando vi帽etas. "
|
| 32 |
+
"Aseg煤rate de que cada punto sea conciso y capture una idea central."
|
| 33 |
+
f"Texto: {text}"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
response = client.chat.completions.create(
|
| 37 |
+
model="gpt-3.5-turbo",
|
| 38 |
+
messages=[
|
| 39 |
+
{"role": "system", "content": prompt_text}
|
| 40 |
+
],
|
| 41 |
+
max_tokens=250,
|
| 42 |
+
n=1,
|
| 43 |
+
stop=None,
|
| 44 |
+
temperature=0.7,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
summary_raw = response.choices[0].message.content.strip()
|
| 48 |
+
summary_points = [line.strip() for line in summary_raw.split('\n') if line.strip()]
|
| 49 |
+
return summary_points
|
| 50 |
+
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"Error al llamar a la API de OpenAI: {e}")
|
| 53 |
+
return ["Error al generar el resumen. Por favor, verifica tu clave de API o intenta de nuevo."]
|
| 54 |
+
|
| 55 |
+
@app.route('/')
|
| 56 |
+
def index():
|
| 57 |
+
return render_template('index.html')
|
| 58 |
+
|
| 59 |
+
@app.route('/summarize', methods=['POST'])
|
| 60 |
+
def summarize():
|
| 61 |
+
if 'file' not in request.files:
|
| 62 |
+
return jsonify({'error': 'No se ha subido ning煤n archivo.'}), 400
|
| 63 |
+
|
| 64 |
+
file = request.files['file']
|
| 65 |
+
if file.filename == '':
|
| 66 |
+
return jsonify({'error': 'No se ha seleccionado ning煤n archivo.'}), 400
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
raw_text = extract_text_from_file(file)
|
| 70 |
+
if not raw_text:
|
| 71 |
+
return jsonify({'error': 'No se pudo extraer texto del archivo.'}), 400
|
| 72 |
+
|
| 73 |
+
summary = generate_summary_openai(raw_text)
|
| 74 |
+
return jsonify({'summary': summary})
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return jsonify({'error': str(e)}), 500
|
| 77 |
+
|
| 78 |
+
if __name__ == '__main__':
|
| 79 |
+
app.run(debug=True)
|