File size: 1,817 Bytes
01b66c4
 
909c82c
01b66c4
 
 
 
909c82c
 
 
805b392
909c82c
 
 
 
01b66c4
909c82c
 
 
 
 
 
 
 
01b66c4
909c82c
 
01b66c4
 
 
909c82c
 
 
01b66c4
 
909c82c
 
 
 
 
01b66c4
909c82c
 
 
01b66c4
 
 
 
 
909c82c
01b66c4
 
 
 
2204fc6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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."