mirror of
https://github.com/JefferyHcool/BiliNote.git
synced 2026-09-05 23:56:59 +08:00
first commit
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
from abc import ABC,abstractmethod
|
||||
|
||||
from app.models.gpt_model import GPTSource
|
||||
|
||||
|
||||
class GPT(ABC):
|
||||
def summarize(self, source:GPTSource )->str:
|
||||
'''
|
||||
|
||||
:param source:
|
||||
:return:
|
||||
'''
|
||||
pass
|
||||
@@ -0,0 +1,59 @@
|
||||
from typing import List
|
||||
from app.gpt.base import GPT
|
||||
from openai import OpenAI
|
||||
from app.gpt.prompt import BASE_PROMPT, AI_SUM, SCREENSHOT
|
||||
from app.gpt.utils import fix_markdown
|
||||
from app.models.gpt_model import GPTSource
|
||||
from app.models.transcriber_model import TranscriptSegment
|
||||
from datetime import timedelta
|
||||
|
||||
|
||||
class DeepSeekGPT(GPT):
|
||||
def __init__(self):
|
||||
from os import getenv
|
||||
self.api_key = getenv("DEEP_SEEK_API_KEY")
|
||||
self.base_url = getenv("DEEP_SEEK_API_BASE_URL")
|
||||
self.model=getenv('DEEP_SEEK_MODEL')
|
||||
print(self.model)
|
||||
self.client = OpenAI(api_key=self.api_key, base_url=self.base_url)
|
||||
self.screenshot = False
|
||||
|
||||
def _format_time(self, seconds: float) -> str:
|
||||
return str(timedelta(seconds=int(seconds)))[2:] # e.g., 03:15
|
||||
|
||||
def _build_segment_text(self, segments: List[TranscriptSegment]) -> str:
|
||||
return "\n".join(
|
||||
f"{self._format_time(seg.start)} - {seg.text.strip()}"
|
||||
for seg in segments
|
||||
)
|
||||
|
||||
def ensure_segments_type(self, segments) -> List[TranscriptSegment]:
|
||||
return [
|
||||
TranscriptSegment(**seg) if isinstance(seg, dict) else seg
|
||||
for seg in segments
|
||||
]
|
||||
|
||||
def create_messages(self, segments: List[TranscriptSegment], title: str,tags:str):
|
||||
content = BASE_PROMPT.format(
|
||||
video_title=title,
|
||||
segment_text=self._build_segment_text(segments),
|
||||
tags=tags
|
||||
)
|
||||
if self.screenshot:
|
||||
print(":需要截图")
|
||||
content += SCREENSHOT
|
||||
print(content)
|
||||
return [{"role": "user", "content": content + AI_SUM}]
|
||||
|
||||
def summarize(self, source: GPTSource) -> str:
|
||||
self.screenshot = source.screenshot
|
||||
source.segment = self.ensure_segments_type(source.segment)
|
||||
messages = self.create_messages(source.segment, source.title,source.tags)
|
||||
response = self.client.chat.completions.create(
|
||||
model=self.model,
|
||||
messages=messages,
|
||||
temperature=0.7
|
||||
)
|
||||
return response.choices[0].message.content.strip()
|
||||
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
from typing import List
|
||||
from app.gpt.base import GPT
|
||||
from openai import OpenAI
|
||||
from app.gpt.prompt import BASE_PROMPT, AI_SUM, SCREENSHOT, LINK
|
||||
from app.gpt.utils import fix_markdown
|
||||
from app.models.gpt_model import GPTSource
|
||||
from app.models.transcriber_model import TranscriptSegment
|
||||
from datetime import timedelta
|
||||
|
||||
|
||||
class OpenaiGPT(GPT):
|
||||
def __init__(self):
|
||||
from os import getenv
|
||||
self.api_key = getenv("OPENAI_API_KEY")
|
||||
self.base_url = getenv("OPENAI_API_BASE_URL")
|
||||
self.model=getenv('OPENAI_MODEL')
|
||||
print(self.model)
|
||||
self.client = OpenAI(api_key=self.api_key, base_url=self.base_url)
|
||||
self.screenshot = False
|
||||
self.link=False
|
||||
|
||||
def _format_time(self, seconds: float) -> str:
|
||||
return str(timedelta(seconds=int(seconds)))[2:] # e.g., 03:15
|
||||
|
||||
def _build_segment_text(self, segments: List[TranscriptSegment]) -> str:
|
||||
return "\n".join(
|
||||
f"{self._format_time(seg.start)} - {seg.text.strip()}"
|
||||
for seg in segments
|
||||
)
|
||||
|
||||
def ensure_segments_type(self, segments) -> List[TranscriptSegment]:
|
||||
return [
|
||||
TranscriptSegment(**seg) if isinstance(seg, dict) else seg
|
||||
for seg in segments
|
||||
]
|
||||
|
||||
def create_messages(self, segments: List[TranscriptSegment], title: str,tags:str):
|
||||
content = BASE_PROMPT.format(
|
||||
video_title=title,
|
||||
segment_text=self._build_segment_text(segments),
|
||||
tags=tags
|
||||
)
|
||||
if self.screenshot:
|
||||
print(":需要截图")
|
||||
content += SCREENSHOT
|
||||
if self.link:
|
||||
print(":需要链接")
|
||||
content += LINK
|
||||
|
||||
print(content)
|
||||
return [{"role": "user", "content": content + AI_SUM}]
|
||||
|
||||
def summarize(self, source: GPTSource) -> str:
|
||||
self.screenshot = source.screenshot
|
||||
self.link = source.link
|
||||
source.segment = self.ensure_segments_type(source.segment)
|
||||
messages = self.create_messages(source.segment, source.title,source.tags)
|
||||
response = self.client.chat.completions.create(
|
||||
model=self.model,
|
||||
messages=messages,
|
||||
temperature=0.7
|
||||
)
|
||||
return response.choices[0].message.content.strip()
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
BASE_PROMPT = '''
|
||||
You are a professional note-taking assistant who excels at summarizing video transcripts into clear, structured, and information-rich notes.
|
||||
|
||||
🎯 Language Requirement:
|
||||
- The notes must be written in **Chinese**.
|
||||
- Proper nouns, technical terms, brand names, and personal names should remain in **English** where appropriate.
|
||||
|
||||
📌 Video Title:
|
||||
{video_title}
|
||||
|
||||
📎 Video Tags:
|
||||
{tags}
|
||||
|
||||
📝 Your Task:
|
||||
Based on the segmented transcript below, generate structured notes in standard **Markdown format**, and follow these principles:
|
||||
|
||||
1. **Complete information**: Record as much relevant detail as possible to ensure comprehensive coverage.
|
||||
2. **Clear structure**: Organize content with logical sectioning. Use appropriate heading levels (`##`, `###`) to summarize key points in each section.
|
||||
3. **Concise wording**: Use accurate, clear, and professional Chinese expressions.
|
||||
4. **Remove irrelevant content**: Omit advertisements, filler words, casual greetings, and off-topic remarks.
|
||||
5. **Keep critical details**: Preserve important facts, examples, conclusions, and recommendations.
|
||||
6. **Readable layout**: Use bullet points where needed, and keep paragraphs reasonably short to enhance readability.
|
||||
7. **Table of Contents**: Generate a table of contents at the top based on the `##` level headings.
|
||||
|
||||
|
||||
⚠️ Output Instructions:
|
||||
- Only return the final **Markdown content**.
|
||||
- Do **not** wrap the output in code blocks like ```` ```markdown ```` or ```` ``` ````.
|
||||
|
||||
|
||||
🎬 Transcript Segments (Format: Start Time - Text):
|
||||
|
||||
---
|
||||
{segment_text}
|
||||
---
|
||||
'''
|
||||
|
||||
LINK='''
|
||||
9. **Add time markers**: THIS IS IMPORTANT For every main heading (`##`), append the starting time of that segment using the format ,start with *Content ,eg: `*Content-[mm:ss]`.
|
||||
|
||||
|
||||
'''
|
||||
AI_SUM='''
|
||||
|
||||
🧠 Final Touch:
|
||||
At the end of the notes, add a professional **AI Summary** in Chinese – a brief conclusion summarizing the whole video.
|
||||
|
||||
|
||||
|
||||
'''
|
||||
|
||||
SCREENSHOT='''
|
||||
8. **Screenshot placeholders**: If a section involves **visual demonstrations, code walkthroughs, UI interactions**, or any content where visuals aid understanding, insert a screenshot cue at the end of that section:
|
||||
- Format: `*Screenshot-[mm:ss]`
|
||||
- Only use it when truly helpful.
|
||||
'''
|
||||
@@ -0,0 +1,59 @@
|
||||
from typing import List
|
||||
from app.gpt.base import GPT
|
||||
from openai import OpenAI
|
||||
from app.gpt.prompt import BASE_PROMPT, AI_SUM, SCREENSHOT
|
||||
from app.gpt.utils import fix_markdown
|
||||
from app.models.gpt_model import GPTSource
|
||||
from app.models.transcriber_model import TranscriptSegment
|
||||
from datetime import timedelta
|
||||
|
||||
|
||||
class QwenGPT(GPT):
|
||||
def __init__(self):
|
||||
from os import getenv
|
||||
self.api_key = getenv("QWEN_API_KEY")
|
||||
self.base_url = getenv("QWEN_API_BASE_URL")
|
||||
self.model=getenv('QWEN_MODEL')
|
||||
print(self.model)
|
||||
self.client = OpenAI(api_key=self.api_key, base_url=self.base_url)
|
||||
self.screenshot = False
|
||||
|
||||
def _format_time(self, seconds: float) -> str:
|
||||
return str(timedelta(seconds=int(seconds)))[2:] # e.g., 03:15
|
||||
|
||||
def _build_segment_text(self, segments: List[TranscriptSegment]) -> str:
|
||||
return "\n".join(
|
||||
f"{self._format_time(seg.start)} - {seg.text.strip()}"
|
||||
for seg in segments
|
||||
)
|
||||
|
||||
def ensure_segments_type(self, segments) -> List[TranscriptSegment]:
|
||||
return [
|
||||
TranscriptSegment(**seg) if isinstance(seg, dict) else seg
|
||||
for seg in segments
|
||||
]
|
||||
|
||||
def create_messages(self, segments: List[TranscriptSegment], title: str,tags:str):
|
||||
content = BASE_PROMPT.format(
|
||||
video_title=title,
|
||||
segment_text=self._build_segment_text(segments),
|
||||
tags=tags
|
||||
)
|
||||
if self.screenshot:
|
||||
print(":需要截图")
|
||||
content += SCREENSHOT
|
||||
print(content)
|
||||
return [{"role": "user", "content": content + AI_SUM}]
|
||||
|
||||
def summarize(self, source: GPTSource) -> str:
|
||||
self.screenshot = source.screenshot
|
||||
source.segment = self.ensure_segments_type(source.segment)
|
||||
messages = self.create_messages(source.segment, source.title,source.tags)
|
||||
response = self.client.chat.completions.create(
|
||||
model=self.model,
|
||||
messages=messages,
|
||||
temperature=0.7
|
||||
)
|
||||
return response.choices[0].message.content.strip()
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
import codecs
|
||||
|
||||
def fix_markdown(markdown: str) -> str:
|
||||
return codecs.decode(markdown, 'unicode_escape')
|
||||
Reference in New Issue
Block a user