Skip to content

Commit 9497a67

Browse files
committed
add utils_trans
1 parent c978685 commit 9497a67

File tree

2 files changed

+91
-16
lines changed

2 files changed

+91
-16
lines changed

pythonkr_backend/curation/utils.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
import os
88

9+
from django.core.files.base import ContentFile
10+
11+
912
class Result(BaseModel):
1013
categories: list[str]
1114

@@ -76,19 +79,3 @@ def categorize_summary(summary: str, categories: list[str]):
7679
)
7780

7881
return response.text()
79-
80-
81-
class TranslatedResult(BaseModel):
82-
title: str = Field(description="The title of the translated article")
83-
slug: str = Field(
84-
description="The URL slug. Do not include the language code. Make it similar to the original URL."
85-
)
86-
description: str = Field(
87-
description="The description of the translated article. Don't mention that it's translated."
88-
)
89-
author: str = Field(description="The author of the translated article")
90-
tags: list[str] = Field(
91-
description="List of Python-related tags inferred from the document."
92-
)
93-
written_date: date = Field(description="The written date of the translated article")
94-
content: str = Field(description="The content of the translated article")
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from pydantic import BaseModel, Field
2+
from pydantic_ai import Agent
3+
from datetime import date, datetime, time, timedelta
4+
5+
from django.core.files.base import ContentFile
6+
7+
class TranslatedResult(BaseModel):
8+
title: str = Field(description="The title of the translated article")
9+
slug: str = Field(
10+
description="The URL slug. Do not include the language code. Make it similar to the original URL."
11+
)
12+
description: str = Field(
13+
description="The description of the translated article. Don't mention that it's translated."
14+
)
15+
author: str = Field(description="The author of the translated article")
16+
tags: list[str] = Field(
17+
description="List of Python-related tags inferred from the document."
18+
)
19+
written_date: date = Field(description="The written date of the translated article")
20+
content: str = Field(description="The content of the translated article")
21+
22+
def translate_rssitem(rss_item_id: int):
23+
from .models import LLMService, LLMUsage, TranslatedContent, RSSItem
24+
"""
25+
Translate an RSS item to Korean using AI and save as TranslatedContent.
26+
27+
Args:
28+
rss_item: RSSItem instance to translate
29+
30+
Returns:
31+
TranslatedContent: The created translated content instance
32+
"""
33+
rss_item = RSSItem.objects.get(id=rss_item_id)
34+
35+
# Get LLM provider and model
36+
provider, model = LLMService.get_llm_provider_model()
37+
if not provider or not model:
38+
raise ValueError("No available LLM service found")
39+
40+
model_name = f"{provider}:{model}"
41+
42+
# Read the crawled content from the file
43+
if not rss_item.crawled_content:
44+
raise ValueError("RSS item has no crawled content")
45+
46+
with rss_item.crawled_content.open('r', encoding='utf-8') as f:
47+
content = f.read()
48+
49+
# Create AI agent for translation
50+
agent = Agent(
51+
model_name,
52+
output_type=TranslatedResult,
53+
system_prompt="Translate the following markdown article in full to korean"
54+
)
55+
56+
# Run translation
57+
result = agent.run_sync(content)
58+
59+
# Create TranslatedContent instance
60+
translated_content = TranslatedContent(
61+
title=result.output.title,
62+
slug=result.output.slug,
63+
description=result.output.description,
64+
author=result.output.author,
65+
tags=result.output.tags,
66+
written_date=result.output.written_date,
67+
model_name=model_name,
68+
source_rss_item=rss_item,
69+
source_url=rss_item.link
70+
)
71+
72+
# Save the translated content to a file
73+
content_file = ContentFile(result.output.content, name=f"{rss_item.id}-translated.md")
74+
translated_content.content.save(f"{rss_item.id}-translated.md", content_file)
75+
76+
# Save the instance
77+
translated_content.save()
78+
79+
# Create LLM usage record
80+
usage = result.usage()
81+
LLMUsage.objects.create(
82+
model_name=model_name,
83+
input_tokens=usage.request_tokens,
84+
output_tokens=usage.response_tokens,
85+
total_tokens=usage.total_tokens
86+
)
87+
88+
return translated_content

0 commit comments

Comments
 (0)