-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathemotion_extractor.py
More file actions
58 lines (43 loc) · 1.58 KB
/
emotion_extractor.py
File metadata and controls
58 lines (43 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import asyncio
import os
from pathlib import Path
from typing import Optional
from pydantic import BaseModel, Field
from agentics import AG
class Emotion(BaseModel):
emotion_category: Optional[str] = Field(
None,
description="This is the type of the recognized emotion such as joy, sadnees, and fear. Return None if no emotion has been spotted.",
)
text_passage: Optional[str] = Field(
None,
description="This is the text passage where the above emotion has been mentioned. Return None if the above category is None",
)
# confidence: Optional[float] = Field(
# 0, description="This is confidence of your assessment on the above information"
# )
class EmotionDetector(BaseModel):
emotions: Optional[list[Emotion]] = None
# full_text: Optional[str] = Field(
# None, description="The original passage of text copied verbatim from the SOURCE"
# )
def split_into_chunks(text, chunk_size=200):
return [text[i : i + chunk_size] for i in range(0, len(text), chunk_size)]
async def main():
emotion_detector = AG(atype=EmotionDetector)
current_file = Path(__file__).resolve()
text = None
with open(
os.path.join(
os.path.dirname(current_file.parent),
"data/emotion_detection/The_Brothers_Karamazov.txt",
)
) as f:
text = f.read()
emotion_detector.verbose_transduction = True
emotions = await (
emotion_detector << split_into_chunks(text, chunk_size=1000)[:100]
)
emotions.pretty_print()
if __name__ == "__main__":
asyncio.run(main())