Junjie96 commited on
Commit
889d8af
·
verified ·
1 Parent(s): 9c18e52

Delete src/agent.py

Browse files
Files changed (1) hide show
  1. src/agent.py +0 -111
src/agent.py DELETED
@@ -1,111 +0,0 @@
1
- import os
2
- import time
3
-
4
- import gradio as gr
5
- from phi.assistant import Assistant
6
- from phi.llm.openai import OpenAIChat
7
- from pydantic import BaseModel
8
-
9
- from .log import logger
10
-
11
- qwen_model = 'qwen-plus'
12
- qwen_api_key = os.getenv("QWEN_DS_API_KEY")
13
- qwen_url = 'https://dashscope.aliyuncs.com/compatible-mode/v1'
14
-
15
-
16
- class TranslateFormat(BaseModel):
17
- en: str
18
-
19
-
20
- class PormptExtendFormat(BaseModel):
21
- prompt: str
22
-
23
-
24
- trans_assist = Assistant(
25
- llm=OpenAIChat(model=qwen_model, max_tokens=3000, temperature=0.3, api_key=qwen_api_key, base_url=qwen_url),
26
- description='你是专业的英语翻译,请将用户输入的一句中文翻译成英文',
27
- output_model=TranslateFormat
28
- )
29
-
30
- requirements = [
31
- "1.对于过于简短的用户输入,在不改变原意及主体外观的前提下,合理补充具象化细节,避免使用过于抽象描述。",
32
- "2.如果涉及人像,完善与用户输入相符的人物外貌、表情、种族、姿态、景别、穿着、影调、质感等方面描述。",
33
- "3.匹配符合用户意图且精准详细的风格描述。如果用户未指定,则根据画面选择最恰当的风格,或使用纪实摄影照片。",
34
- "4.若输入为中文,则整体中文输出;若输入为全英文,则整体英文输出;保留引号中原文以及重要的输入信息,不要改写。",
35
- "5.在语义完整前提下,改写后prompt字数小于200字,避免prompt冗长。",
36
- """
37
- 6.改写后 prompt 示例:
38
- (1) 日系小清新写真照片,扎着双麻花辫的波西米亚小女孩坐在船边。女孩穿着白色方领泡泡袖连衣裙,裙子上有褶皱和纽扣装饰。她皮肤白皙,五官精致,眼泪汪汪直视镜头。她双手扶船,长发刘海遮住部分额头。背景是清澈明亮的户外场景,可见蓝天、山峦和一些干枯植物。高清写实摄影,近景中心对称构图。\n
39
- (2) 二次元厚涂动漫插画,一个猫耳东亚萌妹手持文件夹,怒气冲冲走向电脑。她深紫色爆炸头,红色眼睛,头顶有一个粉色光圈。少女身穿深灰色短裙和浅灰色上衣,腰间系着白色系带,胸前佩戴名牌,居中写着黑体中文"紫阳"。室内背景中摆放了很多办公桌。粗线条的日系赛璐璐风格。近景半身略仰视视角。\n
40
- (3) 美剧艺术海报风格,身穿黄色防护服的老年Walter White坐在金属折叠椅上,头顶无衬线英文写着"Breaking Bad",周围是成堆的美元和蓝色塑料储物箱。他戴着眼镜目光直视镜头,左手拿着一支雪茄,右手放在膝盖上。背景是废弃阴暗的厂房,阳光透过窗户照射进来。画面带有明显颗粒质感纹理。长焦人物平视特写。
41
- (4) CG game concept digital art featuring three giant mutant crocodiles with wide-open mouths, revealing pink tongues and sharp teeth. Their rough skin resembles grayish-white stone. On the back of the crocodile to the left, lush trees, shrubs, and some thorn-like protrusions grow. The background of the scene shows a dusk sky and a shimmering pond. The overall atmosphere is dark and cold, with a tilted composition that creates a strong sense of depth and layers. \n
42
- """
43
- ]
44
-
45
- prompt_writer = Assistant(
46
- llm=OpenAIChat(model=qwen_model, max_tokens=8000, temperature=0.3, api_key=qwen_api_key, base_url=qwen_url),
47
- description="你是一位prompt优化师,旨在将用户输入改写为优质prompt,在不影响原意前提下更高质量生图。",
48
- instructions=requirements,
49
- debug_mode=False,
50
- output_model=PormptExtendFormat,
51
- )
52
-
53
-
54
- def contains_chinese(text):
55
- """
56
- 判断文本中是否包含中文字符(基于 Unicode 范围)
57
- """
58
- # 定义中文的 Unicode 范围(基础汉字 + 扩展区)
59
- cjk_ranges = [
60
- (0x4e00, 0x9fff), # CJK 基本汉字区(含常用汉字)
61
- (0x3400, 0x4dbf), # CJK 扩展 A
62
- (0x20000, 0x2a6df), # CJK 扩展 B
63
- (0x2a700, 0x2b73f), # CJK 扩展 C
64
- (0x2b740, 0x2b81f), # CJK 扩展 D
65
- (0x2b820, 0x2ceaf), # CJK 扩展 E
66
- ]
67
-
68
- for c in text:
69
- code = ord(c)
70
- for start, end in cjk_ranges:
71
- if start <= code <= end:
72
- return True
73
- return False
74
-
75
-
76
- def translate_prompt(prompt, max_attempts=5):
77
- attempts = 0
78
- while attempts < max_attempts:
79
- try:
80
- if contains_chinese(prompt):
81
- res = trans_assist.run(prompt)
82
- logger.info(f"translate Chinese prompt into English: {prompt} -> {res.en}")
83
- return res.en
84
- else:
85
- return prompt
86
- except Exception as e:
87
- print(f"尝试 {attempts + 1} 失败:{e}")
88
- attempts += 1
89
- if attempts < 5:
90
- time.sleep(1) # 延迟重试
91
- raise gr.Error(f"提示词扩写达到最大尝试次数 {max_attempts},任务失败。")
92
-
93
-
94
- def extend_prompt(prompt, max_attempts=5):
95
- attempts = 0
96
- while attempts < max_attempts:
97
- try:
98
- res = prompt_writer.run(prompt, stream=False)
99
- logger.info(f"extend prompt finished: {prompt} -> {res.prompt}.")
100
- return res.prompt
101
- except Exception as e:
102
- print(f"尝试 {attempts + 1} 失败:{e}")
103
- attempts += 1
104
- if attempts < 5:
105
- time.sleep(1) # 延迟重试
106
- raise gr.Error(f"提示词扩写达到最大尝试次数 {max_attempts},任务失败。")
107
-
108
-
109
- if __name__ == '__main__':
110
- # test_trans()
111
- pass