Spaces:
Running
Running
Delete summerizer.py
Browse files- summarizer.py +0 -76
summarizer.py
DELETED
|
@@ -1,76 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from openai import AsyncOpenAI
|
| 3 |
-
import logging
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
-
from urllib.parse import urlparse
|
| 6 |
-
|
| 7 |
-
# Load environment variables
|
| 8 |
-
load_dotenv()
|
| 9 |
-
|
| 10 |
-
# Set up logging
|
| 11 |
-
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 12 |
-
|
| 13 |
-
async def summarize_text(text: str, url: str = "") -> dict:
|
| 14 |
-
"""Summarize text into a title and description using OpenAI's API."""
|
| 15 |
-
try:
|
| 16 |
-
# Get OpenAI API key
|
| 17 |
-
api_key = os.getenv("OPENAI_API_KEY")
|
| 18 |
-
if not api_key:
|
| 19 |
-
logging.error("OpenAI API key not found. Please set OPENAI_API_KEY in .env file.")
|
| 20 |
-
raise ValueError("OpenAI API key is required for summarization.")
|
| 21 |
-
|
| 22 |
-
# Initialize OpenAI client
|
| 23 |
-
client = AsyncOpenAI(api_key=api_key)
|
| 24 |
-
|
| 25 |
-
# Handle empty or short text
|
| 26 |
-
if not text or len(text.strip()) < 20:
|
| 27 |
-
logging.warning(f"Input text is empty or too short: '{text}'. Using URL context.")
|
| 28 |
-
text = f"Content from {url} about news, products, or services."
|
| 29 |
-
|
| 30 |
-
# Simplified prompt
|
| 31 |
-
prompt = (
|
| 32 |
-
f"Summarize the following text into a title (up to 50 characters) and a description (up to 100 characters) "
|
| 33 |
-
f"for RCS messaging. Ensure both are complete and relevant. If the text is short, use the URL ({url}) "
|
| 34 |
-
f"to infer context for a news, product, or service site. Format the output as:\nTitle: [title]\nDescription: [description]\n\n{text}"
|
| 35 |
-
)
|
| 36 |
-
response = await client.chat.completions.create(
|
| 37 |
-
model="gpt-4o-mini",
|
| 38 |
-
messages=[
|
| 39 |
-
{"role": "system", "content": "You are an assistant crafting summaries for RCS messaging."},
|
| 40 |
-
{"role": "user", "content": prompt}
|
| 41 |
-
],
|
| 42 |
-
temperature=0.6
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
# Log raw response
|
| 46 |
-
raw_content = response.choices[0].message.content.strip()
|
| 47 |
-
logging.info(f"Raw LLM response: {raw_content}")
|
| 48 |
-
|
| 49 |
-
# Parse response
|
| 50 |
-
lines = raw_content.split("\n")
|
| 51 |
-
title = "News Summary"
|
| 52 |
-
description = f"Explore content from {urlparse(url).netloc}."[:100]
|
| 53 |
-
|
| 54 |
-
for line in lines:
|
| 55 |
-
if line.startswith("Title:"):
|
| 56 |
-
title = line.replace("Title:", "").strip()[:50]
|
| 57 |
-
elif line.startswith("Description:"):
|
| 58 |
-
description = line.replace("Description:", "").strip()[:100]
|
| 59 |
-
|
| 60 |
-
# Ensure non-empty description
|
| 61 |
-
if not description or description == f"Explore content from {urlparse(url).netloc}.":
|
| 62 |
-
logging.warning("Description is empty or default. Using fallback.")
|
| 63 |
-
description = f"Discover news and insights from {urlparse(url).netloc}."[:100]
|
| 64 |
-
|
| 65 |
-
logging.info(f"Parsed summary - Title: {title}, Description: {description}")
|
| 66 |
-
return {"title": title, "description": description}
|
| 67 |
-
|
| 68 |
-
except Exception as e:
|
| 69 |
-
logging.error(f"Error summarizing text with OpenAI: {e}")
|
| 70 |
-
# Fallback
|
| 71 |
-
description = f"Discover news and insights from {urlparse(url).netloc}."[:100]
|
| 72 |
-
logging.info(f"Using fallback - Title: News Summary, Description: {description}")
|
| 73 |
-
return {
|
| 74 |
-
"title": "News Summary",
|
| 75 |
-
"description": description
|
| 76 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|