apapis added

This commit is contained in:
OwusuBlessing
2025-02-17 21:29:10 +01:00
parent 958f7e2c45
commit 3ea82bce3c
5 changed files with 224 additions and 1 deletions
+61
View File
@@ -21,6 +21,7 @@ import requests
import os
from PyPDF2 import PdfReader
from scripts.transcriber import transcribe_media,group_words_into_sentences # Import the transcribe_media function
from scripts.generate_summary import general_summary,custom_summary
# Load environment variables
load_dotenv()
API_KEY = os.getenv("API_KEY_ACCESS")
@@ -71,6 +72,16 @@ class ChatResp(BaseModel): # Added BaseModel inheritance
class TranscriptResponse(BaseModel):
transcript: dict # Changed type hint for transcript to a dictionary
class GeneralSummaryRequest(BaseModel):
transcript: Optional[str] = None
class TemplateSummaryRequest(BaseModel):
transcript: Optional[str] = None
template: Optional[str] = None
@app.post("/microdot-ai/transcribe")
async def chat_endpoint(
request: TranscribeRequest,
@@ -101,6 +112,56 @@ async def chat_endpoint(
)
@app.post("/microdot-ai/general-summary")
async def general_summary_endpoint(
request: GeneralSummaryRequest,
api_key: str = Depends(get_api_key)
):
try:
if not request.transcript:
raise HTTPException(status_code=400, detail="Transcript is required.")
response = general_summary(json.loads(request.transcript))
return TranscriptResponse(
transcript=response
)
except Exception as e:
print(f"Error processing general summary request: {str(e)}")
raise HTTPException(
status_code=500,
detail=f"Error processing general summary request: {str(e)}"
)
@app.post("/microdot-ai/template-summary")
async def template_summary_endpoint( # Corrected function name to avoid conflict
request: TemplateSummaryRequest,
api_key: str = Depends(get_api_key)
):
try:
if not request.transcript:
raise HTTPException(status_code=400, detail="Transcript is required.")
if not request.template:
raise HTTPException(status_code=400, detail="Template is required.")
transcript = json.loads(request.transcript)
template = json.loads(request.template) # Removed the check for missing template as it's now required
response = custom_summary(template, transcript)
return TranscriptResponse(
transcript=response
)
except Exception as e:
print(f"Error processing template summary request: {str(e)}") # Updated print statement for clarity
raise HTTPException(
status_code=500,
detail=f"Error processing template summary request: {str(e)}"
)
@app.on_event("startup")
async def startup_event():
View File
+42
View File
@@ -0,0 +1,42 @@
import anthropic
import os
from dotenv import load_dotenv
import json
from src.prompt import general_summary_prompt,custom_template_prompt
load_dotenv()
def general_summary(transcription):
client = anthropic.Anthropic(
api_key=os.getenv("ANTHTROPIC_API_KEY"),
)
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=4000,
messages=[
{"role": "user", "content": f"{general_summary_prompt}"},
{"role": "user", "content": f"Transcription: {transcription}"}
]
)
text = message.content[0].text
return json.loads(text)
def custom_summary(template, transcription):
client = anthropic.Anthropic(
api_key=os.getenv("ANTHTROPIC_API_KEY"),
)
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=8000,
messages=[
{"role": "user", "content": f"{custom_template_prompt}"},
{"role": "user", "content": f"TEMPLATE : {template}"},
{"role": "user", "content": f"Transcription: {transcription}"}
]
)
text = message.content[0].text
return json.loads(text)
+1 -1
View File
@@ -123,7 +123,7 @@ def transcribe_media(file_loc: str, media_type: str = "audio"):
else:
raise ValueError("media_type must be either 'audio' or 'video'.")
print(f"Transcription response: {response}\n\n")
return response
except Exception as e:
+120
View File
@@ -0,0 +1,120 @@
general_summary_prompt = """
You are an AI meeting transcript summary formatter. You will be provided with a detailed meeting transcript that includes sentence-level summaries with timestamps (in seconds), speaker details, and word-level timestamps. Your task is to generate a concise summary of the meeting organized into four sections:
1. **Purpose:**
- Provide a brief description of the meeting's purpose.
2. **Chapters:**
- Provide a list of chapter titles that segment the meeting into key parts.
- For each chapter, include a timestamp range (with "start" and "end" in seconds) indicating when that chapter begins and ends.
- Additionally, include a list of word-level timestamps for each word in the chapter. **Important:** For every word in a sentence, the timestamp must be the start timestamp of the sentence to which the word belongs.
3. **Outcomes:**
- Provide a coherent description of the meeting outcomes.
- For each outcome, include a timestamp range (with "start" and "end" in seconds) corresponding to the relevant moment, and include word-level timestamps for each word (using the sentences start timestamp for every word).
4. **Action Items:**
- Provide a list of actionable items derived from the meeting discussion.
- For each action item, include either a single timestamp or a timestamp range (if available) and a list of word-level timestamps for each word (again, each word's timestamp is the start timestamp of its parent sentence).
At the end of each section, include a field named "minutes_total" which represents the total duration in minutes for that section. Calculate this value by using the start time of the first sentence and the end time of the last sentence within the section. If the duration is not a whole number, express it as a decimal (e.g., 0.5).
**Instructions:**
- Return a JSON response containing only the required fields with no additional commentary.
- The JSON output must be properly formatted and valid.
- Do not include any markdown or code block formatting markers (such as ```json) in your output.
- Ensure that for each sentence you generate, every word in that sentence is assigned the same timestamp—the start timestamp of that sentence.
**Example Output JSON:**
{
"Purpose": {
"text": "Discuss project progress and define upcoming milestones."
},
"Chapters": {
"minutes_total": 3,
"content": [
{
"chapter": "Project Overview",
"time_stamp": {"start": 5.12, "end": 5.68},
"words_time_stamp": [
{"word": "Project", "timestamp": 5.12},
{"word": "Overview", "timestamp": 5.12}
]
},
{
"chapter": "Budget Review",
"time_stamp": {"start": 10.50, "end": 11.20},
"words_time_stamp": [
{"word": "Budget", "timestamp": 10.50},
{"word": "Review", "timestamp": 10.50}
]
}
]
},
"Outcomes": {
"minutes_total": 3,
"content": [
{
"text": "Key performance metrics were defined and improvement areas identified.",
"time_stamp": {"start": 15.30, "end": 16.00},
"words_time_stamp": [
{"word": "Key", "timestamp": 15.30},
{"word": "performance", "timestamp": 15.30},
{"word": "metrics", "timestamp": 15.30},
{"word": "were", "timestamp": 15.30},
{"word": "defined", "timestamp": 15.30},
{"word": "and", "timestamp": 15.30},
{"word": "improvement", "timestamp": 15.30},
{"word": "areas", "timestamp": 15.30},
{"word": "identified", "timestamp": 15.30}
]
}
]
},
"Action_Items": {
"minutes_total": 3,
"content": [
{
"text": "Prepare a detailed budget report for the next meeting.",
"time_stamp": {"start": 30.45, "end": 30.45},
"words_time_stamp": [
{"word": "Prepare", "timestamp": 30.45},
{"word": "a", "timestamp": 30.45},
{"word": "detailed", "timestamp": 30.45},
{"word": "budget", "timestamp": 30.45},
{"word": "report", "timestamp": 30.45},
{"word": "for", "timestamp": 30.45},
{"word": "the", "timestamp": 30.45},
{"word": "next", "timestamp": 30.45},
{"word": "meeting", "timestamp": 30.45}
]
}
]
}
}
Remember, every word in each sentence must have a single timestamp equal to the start timestamp of that sentence. Your output must strictly adhere to the provided structure, and the "minutes_total" for each section must be correctly calculated based on the start time of the first sentence and the end time of the last sentence, expressed as a decimal if necessary.
NOTE : start and end time are in seconds , so take that into considerations when calculating the total time in mins
"""
custom_template_prompt = """ You are an AI meeting transcript summary formatter. You will be provided with a sentence-level and word-level summary of a meeting, which includes timestamps for each sentence (in seconds), speaker details, and word-level timestamps. Your task is to generate a structured summary of the meeting based on a user-defined template.
How It Works: The user will provide custom section headers along with descriptions of what each section should contain. You must generate a JSON response that exactly follows the user-defined structure. For each section that includes timestamps, ensure that the timestamps are accurately inferred from the provided sentence and word-level timestamps. For every sentence you generate, assign each word the same timestamp—the start timestamp of the sentence that the word belongs to. Word-level timestamps you generate should reflect the sentences start time for every word. At the end of each section, correctly calculate the total duration in minutes ("minutes_total") based on the start time of the first sentence and the end time of the last sentence. If the total duration is not a whole number, represent it as a decimal (e.g., 0.5 mins).
Instructions:
Return a JSON response containing only the required fields with no additional commentary.
For each section that includes a timestamp, include the timestamp exactly as provided (in seconds).
Include a list of word-level timestamps for each word in the relevant sections.
Ensure the JSON is properly formatted and valid.
Do not include any markdown or code block markers (such as ```json) in your output.
Input Example: { "Key_Points": "Summarize the most critical discussion points from the meeting.", "Summary": "Provide a brief overall summary of what was discussed.", "Next_Steps": "List the next steps decided during the meeting, including any action items." }
Example Output JSON:
{ "Key_Points": { "minutes_total": 3.5, "content": [ { "text": "Introductions between Diane Taylor and Cody Smith.", "time_stamp": {"start": 5.12, "end": 5.68}, "words_time_stamp": [ {"word": "Introductions", "timestamp": 5.12}, {"word": "between", "timestamp": 5.12}, {"word": "Diane", "timestamp": 5.12}, {"word": "Taylor", "timestamp": 5.12}, {"word": "and", "timestamp": 5.12}, {"word": "Cody", "timestamp": 5.12}, {"word": "Smith.", "timestamp": 5.12} ] } ] }, "Summary": { "minutes_total": 3.5, "content": [ { "text": "The meeting started with introductions, followed by a discussion of key topics.", "time_stamp": {"start": 5.12, "end": 10.12}, "words_time_stamp": [ {"word": "The", "timestamp": 5.12}, {"word": "meeting", "timestamp": 5.12}, {"word": "started", "timestamp": 5.12}, {"word": "with", "timestamp": 5.12}, {"word": "introductions,", "timestamp": 5.12}, {"word": "followed", "timestamp": 5.12}, {"word": "by", "timestamp": 5.12}, {"word": "a", "timestamp": 5.12}, {"word": "discussion", "timestamp": 5.12}, {"word": "of", "timestamp": 5.12}, {"word": "key", "timestamp": 5.12}, {"word": "topics.", "timestamp": 5.12} ] } ] }, "Next_Steps": { "minutes_total": 2.0, "content": [ { "text": "Diane will follow up with Cody regarding office management tasks.", "time_stamp": {"start": 30.45, "end": 30.45}, "words_time_stamp": [ {"word": "Diane", "timestamp": 30.45}, {"word": "will", "timestamp": 30.45}, {"word": "follow", "timestamp": 30.45}, {"word": "up", "timestamp": 30.45}, {"word": "with", "timestamp": 30.45}, {"word": "Cody", "timestamp": 30.45}, {"word": "regarding", "timestamp": 30.45}, {"word": "office", "timestamp": 30.45}, {"word": "management", "timestamp": 30.45}, {"word": "tasks.", "timestamp": 30.45} ] } ] } }
Remember, for every sentence generated in any section, every word must be assigned the sentences start timestamp as its "timestamp" value. Additionally, calculate the "minutes_total" for each section by using the start time of the first sentence and the end time of the last sentence; if the result is not a whole number, express it as a decimal (e.g., 0.5 mins). Your output must strictly adhere to the provided structure.
NOTE : start and end time are in seconds , so take that into considerations when calculating the total time in mins"""