DevClivora commited on
Commit
a57c24b
·
verified ·
1 Parent(s): 8c12eef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -26
app.py CHANGED
@@ -1,5 +1,5 @@
1
  # app.py
2
- # A Flask API with extra print statements for debugging on Hugging Face Spaces.
3
 
4
  import os
5
  import pandas as pd
@@ -8,31 +8,35 @@ from flask import Flask, request, jsonify
8
  from flask_cors import CORS
9
  from huggingface_hub import HfApi
10
  from datetime import datetime
 
11
 
12
  # --- Initialization ---
13
- print("--- Flask app.py is starting up! ---")
14
  app = Flask(__name__)
15
  CORS(app)
16
 
 
 
 
 
17
  # --- Configuration ---
18
- print("Loading environment variables...")
19
  try:
20
  HF_TOKEN = os.environ["HF_TOKEN"]
21
  AUDIO_DATASET_REPO_ID = os.environ["AUDIO_DATASET_REPO_ID"]
22
  TEXT_DATASET_REPO_ID = os.environ["TEXT_DATASET_REPO_ID"]
23
- print("Successfully loaded all required environment variables.")
24
  except KeyError as e:
25
- print(f"FATAL ERROR: Missing secret environment variable: {e}")
26
  HF_TOKEN, AUDIO_DATASET_REPO_ID, TEXT_DATASET_REPO_ID = None, None, None
27
 
28
  # --- Hugging Face API Client ---
29
  if HF_TOKEN:
30
- print("Initializing HfApi client...")
31
  api = HfApi(token=HF_TOKEN)
32
- print("HfApi client initialized.")
33
  else:
34
  api = None
35
- print("Warning: HfApi not initialized because HF_TOKEN is not set.")
36
 
37
 
38
  # --- Helper Function ---
@@ -46,7 +50,7 @@ def get_unique_filename():
46
  @app.route('/')
47
  def index():
48
  """A simple index route to confirm the API is running."""
49
- print("Request received for / route.")
50
  return "Hugging Face Data Uploader API is running."
51
 
52
  @app.route('/add-audio', methods=['POST'])
@@ -54,24 +58,24 @@ def add_audio_data():
54
  """
55
  Receives an audio/transcription pair, converts to Parquet, and uploads to the audio dataset.
56
  """
57
- print("Request received for /add-audio route.")
58
  if not api:
59
- print("Error: API client not available.")
60
  return jsonify({"error": "Server is not configured with Hugging Face credentials."}), 500
61
  try:
62
- print("Attempting to process /add-audio data...")
63
  data = request.get_json()
64
  if not data or 'audio' not in data or 'transcription' not in data:
65
- print("Invalid payload received for /add-audio.")
66
  return jsonify({"error": "Invalid payload. 'audio' and 'transcription' fields are required."}), 400
67
 
68
- print("Data validated. Creating DataFrame.")
69
  df = pd.DataFrame([data])
70
  buffer = io.BytesIO()
71
  df.to_parquet(buffer, index=False, engine='pyarrow')
72
  buffer.seek(0)
73
 
74
- print(f"Uploading file to audio dataset: {AUDIO_DATASET_REPO_ID}")
75
  api.upload_file(
76
  path_or_fileobj=buffer,
77
  path_in_repo=get_unique_filename(),
@@ -79,11 +83,11 @@ def add_audio_data():
79
  repo_type="dataset",
80
  commit_message="Add new audio-transcription pair"
81
  )
82
- print("File successfully uploaded to audio dataset.")
83
  return jsonify({"message": "Audio data added successfully."}), 201
84
 
85
  except Exception as e:
86
- print(f"---! UNEXPECTED ERROR in /add-audio !---: {e}")
87
  return jsonify({"error": "An internal error occurred.", "details": str(e)}), 500
88
 
89
 
@@ -92,24 +96,24 @@ def add_text_data():
92
  """
93
  Receives a transcription/summary pair, converts to Parquet, and uploads to the text dataset.
94
  """
95
- print("Request received for /add-text route.")
96
  if not api:
97
- print("Error: API client not available.")
98
  return jsonify({"error": "Server is not configured with Hugging Face credentials."}), 500
99
  try:
100
- print("Attempting to process /add-text data...")
101
  data = request.get_json()
102
  if not data or 'transcription' not in data or 'summary' not in data:
103
- print("Invalid payload received for /add-text.")
104
  return jsonify({"error": "Invalid payload. 'transcription' and 'summary' fields are required."}), 400
105
 
106
- print("Data validated. Creating DataFrame.")
107
  df = pd.DataFrame([data])
108
  buffer = io.BytesIO()
109
  df.to_parquet(buffer, index=False, engine='pyarrow')
110
  buffer.seek(0)
111
 
112
- print(f"Uploading file to text dataset: {TEXT_DATASET_REPO_ID}")
113
  api.upload_file(
114
  path_or_fileobj=buffer,
115
  path_in_repo=get_unique_filename(),
@@ -117,14 +121,14 @@ def add_text_data():
117
  repo_type="dataset",
118
  commit_message="Add new transcription-summary pair"
119
  )
120
- print("File successfully uploaded to text dataset.")
121
  return jsonify({"message": "Text data added successfully."}), 201
122
 
123
  except Exception as e:
124
- print(f"---! UNEXPECTED ERROR in /add-text !---: {e}")
125
  return jsonify({"error": "An internal error occurred.", "details": str(e)}), 500
126
 
127
  # To run on Hugging Face Spaces
128
  if __name__ == '__main__':
129
- print("Starting Flask development server...")
130
  app.run(host='0.0.0.0', port=7860)
 
1
  # app.py
2
+ # A Flask API updated to use the proper Flask logger instead of print().
3
 
4
  import os
5
  import pandas as pd
 
8
  from flask_cors import CORS
9
  from huggingface_hub import HfApi
10
  from datetime import datetime
11
+ import logging
12
 
13
  # --- Initialization ---
 
14
  app = Flask(__name__)
15
  CORS(app)
16
 
17
+ # Configure logging
18
+ app.logger.setLevel(logging.INFO)
19
+ app.logger.info("--- Flask app.py is starting up! ---")
20
+
21
  # --- Configuration ---
22
+ app.logger.info("Loading environment variables...")
23
  try:
24
  HF_TOKEN = os.environ["HF_TOKEN"]
25
  AUDIO_DATASET_REPO_ID = os.environ["AUDIO_DATASET_REPO_ID"]
26
  TEXT_DATASET_REPO_ID = os.environ["TEXT_DATASET_REPO_ID"]
27
+ app.logger.info("Successfully loaded all required environment variables.")
28
  except KeyError as e:
29
+ app.logger.error(f"FATAL ERROR: Missing secret environment variable: {e}")
30
  HF_TOKEN, AUDIO_DATASET_REPO_ID, TEXT_DATASET_REPO_ID = None, None, None
31
 
32
  # --- Hugging Face API Client ---
33
  if HF_TOKEN:
34
+ app.logger.info("Initializing HfApi client...")
35
  api = HfApi(token=HF_TOKEN)
36
+ app.logger.info("HfApi client initialized.")
37
  else:
38
  api = None
39
+ app.logger.warning("Warning: HfApi not initialized because HF_TOKEN is not set.")
40
 
41
 
42
  # --- Helper Function ---
 
50
  @app.route('/')
51
  def index():
52
  """A simple index route to confirm the API is running."""
53
+ app.logger.info("Request received for / route.")
54
  return "Hugging Face Data Uploader API is running."
55
 
56
  @app.route('/add-audio', methods=['POST'])
 
58
  """
59
  Receives an audio/transcription pair, converts to Parquet, and uploads to the audio dataset.
60
  """
61
+ app.logger.info("Request received for /add-audio route.")
62
  if not api:
63
+ app.logger.error("API client not available for /add-audio.")
64
  return jsonify({"error": "Server is not configured with Hugging Face credentials."}), 500
65
  try:
66
+ app.logger.info("Attempting to process /add-audio data...")
67
  data = request.get_json()
68
  if not data or 'audio' not in data or 'transcription' not in data:
69
+ app.logger.warning("Invalid payload received for /add-audio.")
70
  return jsonify({"error": "Invalid payload. 'audio' and 'transcription' fields are required."}), 400
71
 
72
+ app.logger.info("Data validated. Creating DataFrame.")
73
  df = pd.DataFrame([data])
74
  buffer = io.BytesIO()
75
  df.to_parquet(buffer, index=False, engine='pyarrow')
76
  buffer.seek(0)
77
 
78
+ app.logger.info(f"Uploading file to audio dataset: {AUDIO_DATASET_REPO_ID}")
79
  api.upload_file(
80
  path_or_fileobj=buffer,
81
  path_in_repo=get_unique_filename(),
 
83
  repo_type="dataset",
84
  commit_message="Add new audio-transcription pair"
85
  )
86
+ app.logger.info("File successfully uploaded to audio dataset.")
87
  return jsonify({"message": "Audio data added successfully."}), 201
88
 
89
  except Exception as e:
90
+ app.logger.error(f"---! UNEXPECTED ERROR in /add-audio !---: {e}", exc_info=True)
91
  return jsonify({"error": "An internal error occurred.", "details": str(e)}), 500
92
 
93
 
 
96
  """
97
  Receives a transcription/summary pair, converts to Parquet, and uploads to the text dataset.
98
  """
99
+ app.logger.info("Request received for /add-text route.")
100
  if not api:
101
+ app.logger.error("API client not available for /add-text.")
102
  return jsonify({"error": "Server is not configured with Hugging Face credentials."}), 500
103
  try:
104
+ app.logger.info("Attempting to process /add-text data...")
105
  data = request.get_json()
106
  if not data or 'transcription' not in data or 'summary' not in data:
107
+ app.logger.warning("Invalid payload received for /add-text.")
108
  return jsonify({"error": "Invalid payload. 'transcription' and 'summary' fields are required."}), 400
109
 
110
+ app.logger.info("Data validated. Creating DataFrame.")
111
  df = pd.DataFrame([data])
112
  buffer = io.BytesIO()
113
  df.to_parquet(buffer, index=False, engine='pyarrow')
114
  buffer.seek(0)
115
 
116
+ app.logger.info(f"Uploading file to text dataset: {TEXT_DATASET_REPO_ID}")
117
  api.upload_file(
118
  path_or_fileobj=buffer,
119
  path_in_repo=get_unique_filename(),
 
121
  repo_type="dataset",
122
  commit_message="Add new transcription-summary pair"
123
  )
124
+ app.logger.info("File successfully uploaded to text dataset.")
125
  return jsonify({"message": "Text data added successfully."}), 201
126
 
127
  except Exception as e:
128
+ app.logger.error(f"---! UNEXPECTED ERROR in /add-text !---: {e}", exc_info=True)
129
  return jsonify({"error": "An internal error occurred.", "details": str(e)}), 500
130
 
131
  # To run on Hugging Face Spaces
132
  if __name__ == '__main__':
133
+ app.logger.info("Starting Flask development server...")
134
  app.run(host='0.0.0.0', port=7860)