Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -337,11 +337,21 @@ async def _call_gemini(text: str, summary_type: str) -> Tuple[Optional[str], Opt
|
|
337 |
logger.info(f"[Gemini Primary] Generating {summary_type} summary using {GEMINI_MODEL}. Input length: {len(text)}")
|
338 |
|
339 |
# Define prompts
|
340 |
-
if summary_type == "paragraph":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
341 |
else: # points summary
|
342 |
prompt = ("You are an AI model designed to provide concise summaries using British English spellings. Your output MUST strictly follow this format:\n\n"
|
343 |
"β’ For each distinct topic or section identified in the text, create a heading.\n"
|
344 |
-
"β’ Each heading MUST be plain text without any formatting (e.g., Section Title).\n"
|
345 |
"β’ Immediately following each heading, list the key points as a bulleted list.\n"
|
346 |
"β’ Each bullet point MUST start with a hyphen and a space (- ) on a new line.\n"
|
347 |
"β’ The text within each bullet point should NOT contain any bold formatting.\n"
|
@@ -352,7 +362,8 @@ async def _call_gemini(text: str, summary_type: str) -> Tuple[Optional[str], Opt
|
|
352 |
"β’ Keep bullet points concise.\n"
|
353 |
"β’ Ensure the entire summary takes no more than two minutes to read.\n"
|
354 |
"β’ Consider the entire text's content, not just the beginning or a few topics.\n"
|
355 |
-
"β’ Use semicolons (;) instead of em dashes (β or β).\n
|
|
|
356 |
"Here is the text to summarise:")
|
357 |
|
358 |
# Input Length Check (Gemini-specific limits if known, otherwise use a large default)
|
@@ -423,11 +434,21 @@ async def _call_openrouter(text: str, summary_type: str) -> Tuple[Optional[str],
|
|
423 |
logger.info(f"[OpenRouter Fallback] Generating {summary_type} summary using {OPENROUTER_MODEL}. Input length: {len(text)}")
|
424 |
|
425 |
# Define prompts
|
426 |
-
if summary_type == "paragraph":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
427 |
else: # points summary
|
428 |
prompt = ("You are an AI model designed to provide concise summaries using British English spellings. Your output MUST strictly follow this format:\n\n"
|
429 |
"β’ For each distinct topic or section identified in the text, create a heading.\n"
|
430 |
-
"β’ Each heading MUST be plain text without any formatting (e.g., Section Title).\n"
|
431 |
"β’ Immediately following each heading, list the key points as a bulleted list.\n"
|
432 |
"β’ Each bullet point MUST start with a hyphen and a space (- ) on a new line.\n"
|
433 |
"β’ The text within each bullet point should NOT contain any bold formatting.\n"
|
@@ -438,7 +459,8 @@ async def _call_openrouter(text: str, summary_type: str) -> Tuple[Optional[str],
|
|
438 |
"β’ Keep bullet points concise.\n"
|
439 |
"β’ Ensure the entire summary takes no more than two minutes to read.\n"
|
440 |
"β’ Consider the entire text's content, not just the beginning or a few topics.\n"
|
441 |
-
"β’ Use semicolons (;) instead of em dashes (β or β).\n
|
|
|
442 |
"Here is the text to summarise:")
|
443 |
|
444 |
# Input Length Check (OpenRouter-specific limit)
|
@@ -635,13 +657,14 @@ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
|
635 |
user = update.effective_user; mention = user.mention_html()
|
636 |
if not user or not update.message: return
|
637 |
logger.info(f"User {user.id} used /start.")
|
638 |
-
await update.message.reply_html( f"π Hello {mention}! I can
|
639 |
|
640 |
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
641 |
user = update.effective_user
|
642 |
if not user or not update.message: return
|
643 |
logger.info(f"User {user.id} used /help.")
|
644 |
-
help_text = ( "π How to use this bot:\n\n" "1. Send me any YouTube video link or website URL.\n" "2. I'll ask you how you want it
|
|
|
645 |
await update.message.reply_text(help_text, parse_mode=ParseMode.MARKDOWN)
|
646 |
|
647 |
async def handle_potential_url(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
@@ -653,7 +676,7 @@ async def handle_potential_url(update: Update, context: ContextTypes.DEFAULT_TYP
|
|
653 |
context.user_data['url_to_summarize'] = url; context.user_data['original_message_id'] = update.message.message_id
|
654 |
keyboard = [[ InlineKeyboardButton("Paragraph Summary", callback_data="paragraph"), InlineKeyboardButton("Points Summary", callback_data="points") ]]
|
655 |
reply_markup = InlineKeyboardMarkup(keyboard)
|
656 |
-
await update.message.reply_text( f"Okay, I see this link:\n{url}\n\nHow would you like it
|
657 |
|
658 |
async def handle_summary_type_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
659 |
query = update.callback_query
|
@@ -761,7 +784,10 @@ async def health_check(request: Request) -> PlainTextResponse:
|
|
761 |
if ptb_app.running: bot_info = await ptb_app.bot.get_me(); bot_status = f"Running (@{bot_info.username})"
|
762 |
else: bot_status = "Initialized/Not running"
|
763 |
except Exception as e: bot_status = f"Error checking status: {e}"
|
764 |
-
return PlainTextResponse( f"TG Bot
|
|
|
|
|
|
|
765 |
|
766 |
async def telegram_webhook(request: Request) -> Response:
|
767 |
global WEBHOOK_SECRET
|
|
|
337 |
logger.info(f"[Gemini Primary] Generating {summary_type} summary using {GEMINI_MODEL}. Input length: {len(text)}")
|
338 |
|
339 |
# Define prompts
|
340 |
+
if summary_type == "paragraph":
|
341 |
+
prompt = ("You are an AI model designed to provide concise summaries using British English spellings. Your output MUST be:\n"
|
342 |
+
"β’ Clear and simple language suitable for someone unfamiliar with the topic.\n"
|
343 |
+
"β’ Uses British English spellings throughout.\n"
|
344 |
+
"β’ Straightforward and understandable vocabulary; avoid complex terms.\n"
|
345 |
+
"β’ Presented as ONE SINGLE PARAGRAPH.\n"
|
346 |
+
"β’ No more than 85 words maximum; but does not have to be exactly 85.\n"
|
347 |
+
"β’ Considers the entire text content equally.\n"
|
348 |
+
"β’ Uses semicolons (;) instead of em dashes (β or β).\n"
|
349 |
+
"β’ **Focus ONLY on the main content; strictly EXCLUDE information about website features, subscriptions, ads, cookie notices, or navigation elements.**\n\n" # Added instruction
|
350 |
+
"Here is the text to summarise:")
|
351 |
else: # points summary
|
352 |
prompt = ("You are an AI model designed to provide concise summaries using British English spellings. Your output MUST strictly follow this format:\n\n"
|
353 |
"β’ For each distinct topic or section identified in the text, create a heading.\n"
|
354 |
+
"β’ Each heading MUST be plain text without any formatting (e.g., Section Title).\n"
|
355 |
"β’ Immediately following each heading, list the key points as a bulleted list.\n"
|
356 |
"β’ Each bullet point MUST start with a hyphen and a space (- ) on a new line.\n"
|
357 |
"β’ The text within each bullet point should NOT contain any bold formatting.\n"
|
|
|
362 |
"β’ Keep bullet points concise.\n"
|
363 |
"β’ Ensure the entire summary takes no more than two minutes to read.\n"
|
364 |
"β’ Consider the entire text's content, not just the beginning or a few topics.\n"
|
365 |
+
"β’ Use semicolons (;) instead of em dashes (β or β).\n"
|
366 |
+
"β’ **Focus ONLY on the main content; strictly EXCLUDE information about website features, subscriptions, ads, cookie notices, or navigation elements.**\n\n" # Added instruction
|
367 |
"Here is the text to summarise:")
|
368 |
|
369 |
# Input Length Check (Gemini-specific limits if known, otherwise use a large default)
|
|
|
434 |
logger.info(f"[OpenRouter Fallback] Generating {summary_type} summary using {OPENROUTER_MODEL}. Input length: {len(text)}")
|
435 |
|
436 |
# Define prompts
|
437 |
+
if summary_type == "paragraph":
|
438 |
+
prompt = ("You are an AI model designed to provide concise summaries using British English spellings. Your output MUST be:\n"
|
439 |
+
"β’ Clear and simple language suitable for someone unfamiliar with the topic.\n"
|
440 |
+
"β’ Uses British English spellings throughout.\n"
|
441 |
+
"β’ Straightforward and understandable vocabulary; avoid complex terms.\n"
|
442 |
+
"β’ Presented as ONE SINGLE PARAGRAPH.\n"
|
443 |
+
"β’ No more than 85 words maximum; but does not have to be exactly 85.\n"
|
444 |
+
"β’ Considers the entire text content equally.\n"
|
445 |
+
"β’ Uses semicolons (;) instead of em dashes (β or β).\n"
|
446 |
+
"β’ **Focus ONLY on the main content; strictly EXCLUDE information about website features, subscriptions, ads, cookie notices, or navigation elements.**\n\n" # Added instruction
|
447 |
+
"Here is the text to summarise:")
|
448 |
else: # points summary
|
449 |
prompt = ("You are an AI model designed to provide concise summaries using British English spellings. Your output MUST strictly follow this format:\n\n"
|
450 |
"β’ For each distinct topic or section identified in the text, create a heading.\n"
|
451 |
+
"β’ Each heading MUST be plain text without any formatting (e.g., Section Title).\n"
|
452 |
"β’ Immediately following each heading, list the key points as a bulleted list.\n"
|
453 |
"β’ Each bullet point MUST start with a hyphen and a space (- ) on a new line.\n"
|
454 |
"β’ The text within each bullet point should NOT contain any bold formatting.\n"
|
|
|
459 |
"β’ Keep bullet points concise.\n"
|
460 |
"β’ Ensure the entire summary takes no more than two minutes to read.\n"
|
461 |
"β’ Consider the entire text's content, not just the beginning or a few topics.\n"
|
462 |
+
"β’ Use semicolons (;) instead of em dashes (β or β).\n"
|
463 |
+
"β’ **Focus ONLY on the main content; strictly EXCLUDE information about website features, subscriptions, ads, cookie notices, or navigation elements.**\n\n" # Added instruction
|
464 |
"Here is the text to summarise:")
|
465 |
|
466 |
# Input Length Check (OpenRouter-specific limit)
|
|
|
657 |
user = update.effective_user; mention = user.mention_html()
|
658 |
if not user or not update.message: return
|
659 |
logger.info(f"User {user.id} used /start.")
|
660 |
+
await update.message.reply_html( f"π Hello {mention}! I can summarise YouTube links or website URLs.\n\nJust send me a link anytime!" ) # Changed 'summarize'
|
661 |
|
662 |
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
663 |
user = update.effective_user
|
664 |
if not user or not update.message: return
|
665 |
logger.info(f"User {user.id} used /help.")
|
666 |
+
help_text = ( "π How to use this bot:\n\n" "1. Send me any YouTube video link or website URL.\n" "2. I'll ask you how you want it summarised (paragraph or points).\n" # Changed 'summarized'
|
667 |
+
"3. Click the button for your choice.\n" "4. Wait for the summary!\n\n" "I'll try multiple methods to get content if the first one fails (especially for YouTube transcripts).\n\n" "Commands:\n" "`/start` - Display welcome message\n" "`/help` - Show this help message" )
|
668 |
await update.message.reply_text(help_text, parse_mode=ParseMode.MARKDOWN)
|
669 |
|
670 |
async def handle_potential_url(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
|
|
676 |
context.user_data['url_to_summarize'] = url; context.user_data['original_message_id'] = update.message.message_id
|
677 |
keyboard = [[ InlineKeyboardButton("Paragraph Summary", callback_data="paragraph"), InlineKeyboardButton("Points Summary", callback_data="points") ]]
|
678 |
reply_markup = InlineKeyboardMarkup(keyboard)
|
679 |
+
await update.message.reply_text( f"Okay, I see this link:\n{url}\n\nHow would you like it summarised?", reply_markup=reply_markup, disable_web_page_preview=True ) # Changed 'summarized'
|
680 |
|
681 |
async def handle_summary_type_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
682 |
query = update.callback_query
|
|
|
784 |
if ptb_app.running: bot_info = await ptb_app.bot.get_me(); bot_status = f"Running (@{bot_info.username})"
|
785 |
else: bot_status = "Initialized/Not running"
|
786 |
except Exception as e: bot_status = f"Error checking status: {e}"
|
787 |
+
return PlainTextResponse( f"TG Bot Summariser - Status: {bot_status}\n" # Changed 'Summarizer'
|
788 |
+
f"Primary Model: {GEMINI_MODEL if _gemini_primary_enabled else 'N/A (Disabled)'}\n"
|
789 |
+
f"Fallback Model: {OPENROUTER_MODEL if _openrouter_fallback_enabled else 'N/A (Disabled)'}\n"
|
790 |
+
f"Apify Actor: {APIFY_ACTOR_ID if _apify_token_exists else 'N/A (No Token)'}" )
|
791 |
|
792 |
async def telegram_webhook(request: Request) -> Response:
|
793 |
global WEBHOOK_SECRET
|