GuglielmoTor commited on
Commit
1ef06de
·
verified ·
1 Parent(s): 9a99f17

Update posts_categorization.py

Browse files
Files changed (1) hide show
  1. posts_categorization.py +29 -7
posts_categorization.py CHANGED
@@ -16,6 +16,9 @@ class SummaryOutput(BaseModel):
16
  class ClassificationOutput(BaseModel):
17
  category: str
18
 
 
 
 
19
  # Summarize post text
20
  def summarize_post(text):
21
  if pd.isna(text) or text is None:
@@ -32,21 +35,40 @@ def summarize_post(text):
32
  """
33
 
34
  try:
 
 
35
  response = client.chat.completions.create(
36
- model="deepseek-r1-distill-llama-70b",
37
  response_model=SummaryOutput,
38
- messages=[
39
- {"role": "system", "content": "You are a precise summarizer. Only return a JSON object with a 'summary' string."},
40
- {"role": "user", "content": prompt}
41
- ],
42
  temperature=0.3
43
  )
44
  return response.summary
45
- except Exception as e:
46
- print(f"Summarization error: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  return None
48
 
49
 
 
50
  # Classify post summary into structured categories
51
  def classify_post(summary, labels):
52
  if pd.isna(summary) or summary is None:
 
16
  class ClassificationOutput(BaseModel):
17
  category: str
18
 
19
+ PRIMARY_SUMMARIZER_MODEL = "deepseek-r1-distill-llama-70b"
20
+ FALLBACK_SUMMARIZER_MODEL = "llama-3.3-70b-versatile"
21
+
22
  # Summarize post text
23
  def summarize_post(text):
24
  if pd.isna(text) or text is None:
 
35
  """
36
 
37
  try:
38
+ # Attempt with primary model
39
+ print(f"Attempting summarization with primary model: {PRIMARY_SUMMARIZER_MODEL}")
40
  response = client.chat.completions.create(
41
+ model=PRIMARY_SUMMARIZER_MODEL,
42
  response_model=SummaryOutput,
43
+ messages=messages,
 
 
 
44
  temperature=0.3
45
  )
46
  return response.summary
47
+ except RateLimitError:
48
+ print(f"Rate limit hit for primary summarizer model: {PRIMARY_SUMMARIZER_MODEL}. Trying fallback: {FALLBACK_SUMMARIZER_MODEL}")
49
+ try:
50
+ # Attempt with fallback model
51
+ response = client.chat.completions.create(
52
+ model=FALLBACK_SUMMARIZER_MODEL,
53
+ response_model=SummaryOutput,
54
+ messages=messages,
55
+ temperature=0.3 # Keep temperature consistent or adjust as needed for fallback
56
+ )
57
+ print(f"Summarization successful with fallback model: {FALLBACK_SUMMARIZER_MODEL}")
58
+ return response.summary
59
+ except RateLimitError as rle_fallback:
60
+ print(f"Rate limit hit for fallback summarizer model ({FALLBACK_SUMMARIZER_MODEL}): {rle_fallback}. Summarization failed.")
61
+ return None
62
+ except Exception as e_fallback:
63
+ print(f"Error during summarization with fallback model ({FALLBACK_SUMMARIZER_MODEL}): {e_fallback}")
64
+ return None
65
+ except Exception as e_primary:
66
+ print(f"Error during summarization with primary model ({PRIMARY_SUMMARIZER_MODEL}): {e_primary}")
67
+ # You could also try fallback here for non-rate-limit errors if desired
68
  return None
69
 
70
 
71
+
72
  # Classify post summary into structured categories
73
  def classify_post(summary, labels):
74
  if pd.isna(summary) or summary is None: