Wie kann man kreisförmige Import und Aufgabe, die keine Probleme in der Integration von Flaschen und Sellerie beheben?Python

Python-Programme
Anonymous
 Wie kann man kreisförmige Import und Aufgabe, die keine Probleme in der Integration von Flaschen und Sellerie beheben?

Post by Anonymous »

Ich arbeite an einer Flask -Anwendung, die Sellerie zur Verarbeitung von Hintergrundaufgaben verwendet. Ich stoße auf Probleme mit kreisförmigen Importen und Aufgaben, die keine Fehler gefunden haben. Hier ist die Struktur meines Projekts: < /p>

[*]App.py: Hauptflächenanwendungsdatei < /li>
tasks.py: Sellerie -Task -Definitionen < /li>

< /> < /ulery_config.py: ssly configuration < /li> < /< /> < /> < /ul. />
app.py:

Code: Select all

import os
import subprocess
import xml.etree.ElementTree as ET
from flask import Flask, request, render_template, redirect, flash, jsonify, url_for
from openai import OpenAI
from dotenv import load_dotenv
from werkzeug.utils import secure_filename
import time
from openai import RateLimitError
from celery_config import make_celery
from tasks import process_ttml  # Import the process_ttml task

load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
client = OpenAI(api_key=OPENAI_API_KEY)

app = Flask(__name__)
# Start monitor_ttml.py as a background process
subprocess.Popen(["python", "monitor_ttml.py"])

app.secret_key = 'supersecretkey'
app.config['UPLOAD_FOLDER'] = './uploads'
app.config['ALLOWED_EXTENSIONS'] = {'ttml'}

if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.makedirs(app.config['UPLOAD_FOLDER'])

app.config.update(
CELERY_BROKER_URL='redis://localhost:6379/0',
CELERY_RESULT_BACKEND='redis://localhost:6379/0'
)

celery = make_celery(app)

def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']

def format_timestamp(seconds):
"""Format seconds into HH:MM:SS format."""
h = int(seconds // 3600)
m = int((seconds % 3600) // 60)
s = int(seconds % 60)
return f"{h:02}:{m:02}:{s:02}"

def extract_transcript(ttml_content, include_timestamps=False):
"""Extract transcript from TTML content."""
try:
root = ET.fromstring(ttml_content)
transcript = []

# Find all  elements in the TTML file
paragraphs = root.findall(".//{http://www.w3.org/ns/ttml}p")

for paragraph in paragraphs:
paragraph_text = ""
for span in paragraph.findall(".//{http://www.w3.org/ns/ttml}span"):
if span.text:
paragraph_text += span.text.strip() + " "

paragraph_text = paragraph_text.strip()
if paragraph_text:
if include_timestamps and "begin" in paragraph.attrib:
timestamp = format_timestamp(float(paragraph.attrib["begin"].replace("s", "")))
transcript.append(f"[{timestamp}] {paragraph_text}")
else:
transcript.append(paragraph_text)

return "\n\n".join(transcript)

except ET.ParseError as e:
return f"Error parsing TTML file: {e}"

def summarize_transcript(transcript):
max_chunk_size = 2000  # Adjust this value based on your token limit
transcript_chunks = [transcript[i:i + max_chunk_size] for i in range(0, len(transcript), max_chunk_size)]
summaries = []

for chunk in transcript_chunks:
retry_count = 0
while retry_count < 5:  # Retry up to 5 times
try:
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant that summarizes podcast transcripts."},
{"role": "user", "content": f"Summarize the following podcast transcript in bullet points:\n\n{chunk}"}
],
max_tokens=200
)
summaries.append(response.choices[0].message.content.strip())
break  # Exit the retry loop if successful
except RateLimitError as e:
retry_count += 1
wait_time = 2 ** retry_count  # Exponential backoff
print(f"Rate limit exceeded.  Retrying in {wait_time} seconds...")
time.sleep(wait_time)
except Exception as e:
print(f"An error occurred: {e}")
break

return "\n\n".join(summaries)

@app.route('/')
def index():
return render_template('index.html')

@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
include_timestamps = 'timestamps' in request.form
task = process_ttml.delay(file_path, include_timestamps)
return redirect(url_for('task_status', task_id=task.id))
else:
flash('Invalid file type')
return redirect(request.url)

@app.route('/status/')
def task_status(task_id):
task = process_ttml.AsyncResult(task_id)
if task.state == 'PENDING':
response = {
'state': task.state,
'status': 'Pending...'
}
elif task.state != 'FAILURE':
response = {
'state': task.state,
'result': task.result
}
else:
response = {
'state': task.state,
'status': str(task.info)
}
return jsonify(response)

@app.route('/upload_api', methods=['POST'])
def upload_api():
if 'file' not in request.files:
return jsonify({"error": "No file part"}), 400
file = request.files['file']
if file.filename == '':
return jsonify({"error": "No selected file"}), 400
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
with open(file_path, 'r', encoding='utf-8') as f:
ttml_content = f.read()
transcript = extract_transcript(ttml_content)
summary = summarize_transcript(transcript)
return jsonify({"summary": summary})
else:
return jsonify({"error": "Invalid file type"}), 400

if __name__ == "__main__":
app.run(debug=True)
Tasks.py:

Code: Select all

from celery_config import make_celery
from app import app, extract_transcript, summarize_transcript
from openai.error import RateLimitError

celery = make_celery(app)

@celery.task(bind=True, max_retries=5)
def process_ttml(self, file_path, include_timestamps):
try:
with open(file_path, 'r', encoding='utf-8') as f:
ttml_content = f.read()
transcript = extract_transcript(ttml_content, include_timestamps)
summary = summarize_transcript(transcript)
return summary
except RateLimitError as e:
self.retry(countdown=2 ** self.request.retries, exc=e)
except Exception as e:
raise self.retry(exc=e)
celery_config.py:

Code: Select all

from celery import Celery

def make_celery(app):
celery = Celery(
app.import_name,
backend=app.config['CELERY_RESULT_BACKEND'],
broker=app.config['CELERY_BROKER_URL']
)
celery.conf.update(app.config)
return celery
Problem:
Wenn ich die Flash -Anwendung ausführe, erhalte ich den folgenden Fehler:

Code: Select all

NameError: name 'process_ttml' is not defined
Wenn ich versuche, dies durch Importieren von Process_ttml in App.py zu beheben, erhalte ich einen kreisförmigen Importfehler.

Code: Select all

celery -A tasks.celery worker --loglevel=info
Usage: celery [OPTIONS] COMMAND [ARGS]...
Try 'celery --help' for help.

Error: Invalid value for '-A' / '--app':
Unable to load celery application.
While trying to load the module tasks.celery the following error occurred:
Traceback (most recent call last):
File "/Users/username/Local Documents/python_environments/pytorchenv/lib/python3.11/site-packages/celery/bin/celery.py", line 58, in convert
return find_app(value)
^^^^^^^^^^^^^^^
File "/Users/username/Local Documents/python_environments/pytorchenv/lib/python3.11/site-packages/celery/app/utils.py", line 383, in find_app
sym = symbol_by_name(app, imp=imp)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/username/Local Documents/python_environments/pytorchenv/lib/python3.11/site-packages/kombu/utils/imports.py", line 59, in symbol_by_name
module = imp(module_name, package=package, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/username/Local Documents/python_environments/pytorchenv/lib/python3.11/site-packages/celery/utils/imports.py", line 109, in import_from_cwd
return imp(module, package=package)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/[email protected]/3.11.9_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "", line 1204, in _gcd_import
File "", line 1176, in _find_and_load
File "", line 1147, in _find_and_load_unlocked
File "", line 690, in _load_unlocked
File "", line 940, in exec_module
File "", line 241, in _call_with_frames_removed
File "/Users/username/Documents/Python Scripts/apple-podcast-transcript-extractor/tasks.py", line 3, in 
from app import app, extract_transcript, summarize_transcript
File "/Users/username/Documents/Python Scripts/apple-podcast-transcript-extractor/app.py", line 12, in 
from tasks import process_ttml
ImportError: cannot import name 'process_ttml' from partially initialized module 'tasks' (most likely due to a circular import) (/Users/username/Documents/Python Scripts/apple-podcast-transcript-extractor/tasks.py)
< /code>
[list]
process_ttml
nicht gefunden
[/list]

Code: Select all

NameError

NameError: name 'process_ttml' is not defined
Traceback (most recent call last)

File "/Users/username/Local Documents/python_environments/pytorchenv/lib/python3.11/site-packages/flask/app.py", line 1498, in __call__

return self.wsgi_app(environ, start_response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/Users/username/Local Documents/python_environments/pytorchenv/lib/python3.11/site-packages/flask/app.py", line 1476, in wsgi_app

response = self.handle_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^

File "/Users/username/Local Documents/python_environments/pytorchenv/lib/python3.11/site-packages/flask/app.py", line 1473, in wsgi_app

response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/Users/username/Local Documents/python_environments/pytorchenv/lib/python3.11/site-packages/flask/app.py", line 882, in full_dispatch_request

rv = self.handle_user_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/Users/username/Local Documents/python_environments/pytorchenv/lib/python3.11/site-packages/flask/app.py", line 880, in full_dispatch_request

rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^

File "/Users/username/Local Documents/python_environments/pytorchenv/lib/python3.11/site-packages/flask/app.py", line 865, in dispatch_request

return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/Users/username/Documents/Python Scripts/apple-podcast-transcript-extractor/app.py", line 121, in upload_file

task = process_ttml.delay(file_path, include_timestamps)
^^^^^^^^^^^^

NameError: name 'process_ttml' is not defined

The debugger caught an exception in your WSGI application.  You can now look at the traceback which led to the error.

To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

dump() shows all variables in the frame
dump(obj) dumps all that's known about the object

Brought to you by DON'T PANIC, your friendly Werkzeug powered traceback interpreter.

Frage:
Wie kann ich das kreisförmige Importproblem beheben und sicherstellen, dass die Aufgabe process_ttml korrekt definiert ist und in meiner Flask Anwendung mit Sellerie verwendet wird. Message Broker für Sellerie.
Der Redis -Server wird ausgeführt und zugänglich.>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post