TheOCEAN's picture
Add application file
2204fc6 verified
Raw
History Blame Contribute Delete
1.82 kB
import pickle
from flask import Flask, request, jsonify
from huggingface_hub import hf_hub_download # <-- This is the key function
# Initialize the Flask application
app = Flask(__name__)
# --- Load the Model From Your Other Repository ---
# Define the location of your model files
MODEL_REPO_ID = "TheOCEAN/My_Text_Summarizer"
PICKLE_FILENAME = "summarization_model.pkl"
summarizer_pipeline = None
print("Attempting to load model...")
try:
# This line downloads the 2.12 GB file to this Space's temporary storage
model_path = hf_hub_download(repo_id=MODEL_REPO_ID, filename=PICKLE_FILENAME)
with open(model_path, 'rb') as f:
# Your notebook pickles the entire fine-tuned pipeline
summarizer_pipeline = pickle.load(f)
print("✅ Model pipeline loaded successfully.")
except Exception as e:
print(f"❌ Error loading model from repository: {e}")
# --- API Route ---
@app.route('/summarize', methods=['POST'])
def summarize_endpoint():
if not summarizer_pipeline:
return jsonify({'error': 'Model is not available or failed to load.'}), 500
try:
json_data = request.get_json()
text = json_data.get('text', '')
if not text:
return jsonify({'error': 'No text provided in the request.'}), 400
# Use the summarizer pipeline function
# Based on your notebook, it appears to be a direct callable function
summary = summarizer_pipeline(text)
return jsonify({'summary': summary})
except Exception as e:
print(f"An error occurred during summarization: {e}")
return jsonify({'error': 'Failed to process the summarization request.'}), 500
# A simple root route to check if the app is running
@app.route('/')
def home():
return "Hugging Face Summarizer API is running fine."