| import os |
| from huggingface_hub import InferenceClient |
|
|
| class SummarizationService: |
| """Service for summarizing text and extracting action items.""" |
| |
| def __init__(self): |
| self.summarization_model = "facebook/bart-large-cnn" |
| self.client = InferenceClient( |
| provider="hf-inference", |
| token=os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_TOKEN") |
| ) |
| |
| def _load_prompt(self, prompt_name: str) -> str: |
| """Load prompt template from file.""" |
| try: |
| with open(f"prompts/{prompt_name}.txt", "r") as f: |
| return f.read().strip() |
| except FileNotFoundError: |
| if prompt_name == "summary": |
| return "Summarize the following meeting transcript in a concise manner:\n\n{transcript}" |
| elif prompt_name == "action_items": |
| return "Extract action items from the following meeting transcript. List each action item clearly:\n\n{transcript}" |
| return "{transcript}" |
| |
| def summarize(self, transcript: str) -> str: |
| """Generate a summary of the transcript.""" |
| try: |
| if not transcript.strip(): |
| return "No transcript available to summarize" |
| |
| result = self.client.summarization( |
| transcript, |
| model=self.summarization_model |
| ) |
| |
| return result if result else "Failed to generate summary" |
| |
| except Exception as e: |
| print(f"Summarization error: {e}") |
| return f"Error generating summary: {str(e)}" |
| |
| def extract_action_items(self, transcript: str) -> str: |
| """Extract action items from the transcript.""" |
| try: |
| if not transcript.strip(): |
| return "No transcript available to extract action items" |
| |
| |
| prompt_template = self._load_prompt("action_items") |
| prompt = prompt_template.format(transcript=transcript) |
| |
| result = self.client.summarization( |
| prompt, |
| model=self.summarization_model |
| ) |
| |
| return result if result else "No action items found" |
| |
| except Exception as e: |
| print(f"Action items extraction error: {e}") |
| return f"Error extracting action items: {str(e)}" |