Spaces:
Sleeping
Sleeping
Surbhi
commited on
Commit
·
a238751
1
Parent(s):
26272ae
Using BERT and GPT-4
Browse files- Dockerfile +20 -0
- app.py +55 -0
- requirements.txt +6 -0
Dockerfile
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use Python 3.9
|
2 |
+
FROM python:3.9
|
3 |
+
|
4 |
+
# Create a non-root user
|
5 |
+
RUN useradd -m -u 1000 user
|
6 |
+
USER user
|
7 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
8 |
+
|
9 |
+
# Set working directory
|
10 |
+
WORKDIR /app
|
11 |
+
|
12 |
+
# Copy requirements and install dependencies
|
13 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
14 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
15 |
+
|
16 |
+
# Copy the rest of the application
|
17 |
+
COPY --chown=user . /app
|
18 |
+
|
19 |
+
# Run the FastAPI app with Uvicorn
|
20 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from openai import OpenAI # Use only this import
|
3 |
+
from fastapi import FastAPI, HTTPException
|
4 |
+
from pydantic import BaseModel
|
5 |
+
from transformers import pipeline
|
6 |
+
from keybert import KeyBERT # For BERT-based keyword extraction
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
# Load BERT model for key point extraction
|
11 |
+
kw_model = KeyBERT()
|
12 |
+
|
13 |
+
# Get OpenAI API key from environment variable
|
14 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
15 |
+
if not OPENAI_API_KEY:
|
16 |
+
raise ValueError("OpenAI API key is missing. Set OPENAI_API_KEY as an environment variable.")
|
17 |
+
|
18 |
+
# Initialize OpenAI client
|
19 |
+
client = OpenAI(api_key=OPENAI_API_KEY)
|
20 |
+
|
21 |
+
# Define request format
|
22 |
+
class SummarizationRequest(BaseModel):
|
23 |
+
text: str
|
24 |
+
|
25 |
+
@app.post("/summarize")
|
26 |
+
def summarize_text(request: SummarizationRequest):
|
27 |
+
if not request.text.strip():
|
28 |
+
raise HTTPException(status_code=400, detail="No text provided")
|
29 |
+
|
30 |
+
# Step 1: Extract key points using BERT (KeyBERT)
|
31 |
+
key_points = kw_model.extract_keywords(request.text, keyphrase_ngram_range=(1, 2), stop_words='english', top_n=5)
|
32 |
+
extracted_points = ", ".join([kp[0] for kp in key_points])
|
33 |
+
|
34 |
+
# Step 2: Generate summary using GPT-4
|
35 |
+
try:
|
36 |
+
response = client.chat.completions.create(
|
37 |
+
model="gpt-4",
|
38 |
+
messages=[
|
39 |
+
{"role": "system", "content": "You are an AI assistant that summarizes text."},
|
40 |
+
{"role": "user", "content": f"Summarize the following text in a concise way:\n\n{request.text}"}
|
41 |
+
]
|
42 |
+
)
|
43 |
+
summary = response.choices[0].message.content # Correct response parsing
|
44 |
+
|
45 |
+
except Exception as e:
|
46 |
+
raise HTTPException(status_code=500, detail=f"Error with GPT-4 API: {str(e)}")
|
47 |
+
|
48 |
+
return {
|
49 |
+
"key_points": extracted_points,
|
50 |
+
"summary": summary
|
51 |
+
}
|
52 |
+
|
53 |
+
@app.get("/")
|
54 |
+
def greet_json():
|
55 |
+
return {"message": "Welcome to the AI Summarizer API!"}
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn[standard]
|
3 |
+
openai>=1.0.0
|
4 |
+
transformers
|
5 |
+
keybert
|
6 |
+
sentence-transformers
|