Spaces:
Sleeping
Sleeping
File size: 1,856 Bytes
a238751 |
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 56 |
import os
from openai import OpenAI # Use only this import
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import pipeline
from keybert import KeyBERT # For BERT-based keyword extraction
app = FastAPI()
# Load BERT model for key point extraction
kw_model = KeyBERT()
# Get OpenAI API key from environment variable
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
if not OPENAI_API_KEY:
raise ValueError("OpenAI API key is missing. Set OPENAI_API_KEY as an environment variable.")
# Initialize OpenAI client
client = OpenAI(api_key=OPENAI_API_KEY)
# Define request format
class SummarizationRequest(BaseModel):
text: str
@app.post("/summarize")
def summarize_text(request: SummarizationRequest):
if not request.text.strip():
raise HTTPException(status_code=400, detail="No text provided")
# Step 1: Extract key points using BERT (KeyBERT)
key_points = kw_model.extract_keywords(request.text, keyphrase_ngram_range=(1, 2), stop_words='english', top_n=5)
extracted_points = ", ".join([kp[0] for kp in key_points])
# Step 2: Generate summary using GPT-4
try:
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are an AI assistant that summarizes text."},
{"role": "user", "content": f"Summarize the following text in a concise way:\n\n{request.text}"}
]
)
summary = response.choices[0].message.content # Correct response parsing
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error with GPT-4 API: {str(e)}")
return {
"key_points": extracted_points,
"summary": summary
}
@app.get("/")
def greet_json():
return {"message": "Welcome to the AI Summarizer API!"}
|