Spaces:
Sleeping
Sleeping
| """Prompt template management service.""" | |
| from typing import Dict | |
| from writing_studio.utils.logging import logger | |
| class PromptService: | |
| """Service for managing and generating prompts.""" | |
| def __init__(self): | |
| """Initialize the prompt service with templates for instruction-following models.""" | |
| # Optimized for FLAN-T5 and other instruction-tuned models | |
| self.prompt_packs = { | |
| "General": { | |
| "instruction": "Revise the following text to improve clarity, conciseness, and readability", | |
| "context": "Make it clear and easy to understand while maintaining the original meaning.", | |
| }, | |
| "Literature": { | |
| "instruction": "Revise this literary analysis to strengthen the argument with better evidence and literary terminology", | |
| "context": "Enhance academic rigor and use of textual support.", | |
| }, | |
| "Tech Comm": { | |
| "instruction": "Revise this technical document for precision, clarity, and professional tone", | |
| "context": "Make it accurate, clear, and appropriate for technical audiences.", | |
| }, | |
| "Academic": { | |
| "instruction": "Revise this academic writing to improve formal tone, organization, and scholarly voice", | |
| "context": "Ensure formal register and proper academic style.", | |
| }, | |
| "Creative": { | |
| "instruction": "Revise this creative writing to enhance imagery, voice, and reader engagement", | |
| "context": "Improve descriptive language and narrative flow.", | |
| }, | |
| } | |
| def get_available_packs(self) -> list: | |
| """ | |
| Get list of available prompt packs. | |
| Returns: | |
| List of prompt pack names | |
| """ | |
| return list(self.prompt_packs.keys()) | |
| def generate_prompt(self, user_text: str, pack_name: str = "General") -> str: | |
| """ | |
| Generate a complete prompt from user text and pack template. | |
| Optimized for instruction-following models like FLAN-T5. | |
| Args: | |
| user_text: User's input text | |
| pack_name: Name of the prompt pack to use | |
| Returns: | |
| Complete prompt string | |
| """ | |
| if pack_name not in self.prompt_packs: | |
| logger.warning(f"Unknown prompt pack: {pack_name}, using General") | |
| pack_name = "General" | |
| pack = self.prompt_packs[pack_name] | |
| logger.info(f"Generating prompt with pack: {pack_name}") | |
| # Format optimized for FLAN-T5 and similar instruction-tuned models | |
| prompt = f"{pack['instruction']}. {pack['context']}\n\nText: {user_text}\n\nRevised text:" | |
| return prompt | |
| def add_custom_pack(self, name: str, instruction: str, context: str) -> None: | |
| """ | |
| Add a custom prompt pack. | |
| Args: | |
| name: Pack name | |
| instruction: Main instruction | |
| context: Additional context | |
| """ | |
| self.prompt_packs[name] = { | |
| "instruction": instruction, | |
| "context": context, | |
| } | |
| logger.info(f"Added custom prompt pack: {name}") | |
| def get_pack_info(self, pack_name: str) -> Dict[str, str]: | |
| """ | |
| Get information about a specific prompt pack. | |
| Args: | |
| pack_name: Name of the pack | |
| Returns: | |
| Pack information dictionary | |
| """ | |
| return self.prompt_packs.get( | |
| pack_name, {"instruction": "Unknown pack", "context": ""} | |
| ) | |