Starlucas0201 commited on
Commit
3347c6f
·
verified ·
1 Parent(s): ab687a8

Upload script.py

Browse files
Files changed (1) hide show
  1. script.py +150 -0
script.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import io
4
+ import argparse
5
+ import shutil
6
+ sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
7
+ from gradio_client import Client, handle_file
8
+ from huggingface_hub import HfApi
9
+ from openai import OpenAI
10
+ from dotenv import load_dotenv
11
+ import shutil
12
+
13
+
14
+ #get the api keys from google drive
15
+ load_dotenv(dotenv_path="/mnt/mediaefs/.env")
16
+ openAI_api_key = os.getenv("OPENAI_API_KEY")
17
+ huggingface_api_key = os.getenv("HUGGINGFACE_API_KEY")
18
+ client = OpenAI(api_key=openAI_api_key)
19
+
20
+ #download one image from google drive and submit to huggingface
21
+ def faces_gdrive_to_hf():
22
+
23
+ #obtain one artist face image from the face folder, then delete the image after use
24
+ #extract the artist name
25
+ faces_dir = "/mnt/mediaefs/faces"
26
+ found_image = None
27
+
28
+ for filename in os.listdir(faces_dir):
29
+ if filename.lower().endswith((".jpg", ".jpeg", ".png")):
30
+ found_image = filename
31
+ break
32
+
33
+ if found_image:
34
+ src_path = os.path.join(faces_dir, found_image)
35
+ dest_path = os.path.join(".", found_image)
36
+
37
+ shutil.copy(src_path, dest_path)
38
+ os.remove(src_path)
39
+
40
+ print(f"Downloaded and deleted: {found_image}")
41
+ else:
42
+ print("No image found in faces folder.")
43
+
44
+ #upload iamge to huggingface
45
+ api = HfApi()
46
+ api.upload_file(
47
+ path_or_fileobj=dest_path,
48
+ path_in_repo=found_image,
49
+ repo_id="yijin928/ComfyUI-dataset",
50
+ repo_type="dataset",
51
+ token=huggingface_api_key
52
+ )
53
+ artist=found_image[:-1]
54
+ return f"https://huggingface.co/datasets/yijin928/ComfyUI-dataset/resolve/main/{found_image}", artist
55
+
56
+
57
+ #generate a rednote post from the artist name, generate a prompt from the artist name
58
+ def generate_post_and_prompt(artist_name):
59
+ response = client.chat.completions.create(
60
+ model="gpt-4o",
61
+ messages=[
62
+ {"role": "system", "content": "你是一位擅长创作小红书爆款内容的专家,专门撰写音乐人介绍类文章。请遵循以下步骤进行创作,以吸引用户并提升互动率。\n\n写作要求\n目标:以生动、有趣、引人入胜的方式介绍音乐人及其作品,激发用户的兴趣和讨论。\n\n风格:内容要轻松有趣、口语化,避免生硬的介绍,像和朋友聊天一样。\n\n互动性:鼓励读者留言、分享或参与讨论,让他们对该音乐人产生兴趣。\n\n内容结构\n\n标题要求\n请根据以下技巧生成 1 个富有吸引力的标题,并适当加入 emoji 表情:\n二极管标题法(对比、冲突、反差感)\n热门关键词(从流行关键词列表随机选 1-2 个)\n制造悬念或好奇心(让读者想点进来一探究竟)\n符合小红书风格,简短、有趣、抓人眼球\n使用音乐人昵称或中文名(让读者更容易共鸣)\n\n正文写作技巧\n先将接收到的英文名翻译成中文,优先使用昵称,如果没有,则使用翻译后的中文名。\n正文需包含 3-5 个自然段,每段需配合 emoji 突出重点,遵循以下原则:\n开篇吸引人:用一句有趣、神秘或情感共鸣的话引入,勾起读者兴趣。\n音乐人介绍:突出其特色、代表作、风格,避免流水账式的描述。\n制造情绪共鸣:可结合故事、听众评价或音乐背后的故事。\n互动引导:结尾引导读者留言,比如询问他们的听歌感受或推荐其他类似的音乐人。\n使用爆款关键词:确保内容符合 SEO,增强曝光度。\n标签优化:从文章中提取 3-6 个高相关度的关键词生成 #标签,并放在文末。\n\n最终输出格式\n请按以下格式输出,不包含额外解释:\n标题\n[标题]\n[换行]\n正文\n[正文]\n标签:[标签]"},
63
+ {"role": "user", "content": "Taylor Swift"},
64
+ {"role": "assistant", "content": "🎤霉霉的故事:从乡村小花到国际巨星✨\n\n🎤提到霉霉,你会想到她哪首歌呢?🎵从《Love Story》到《Blank Space》,她的音乐陪伴了无数人的成长。\n\n🎶霉霉不仅是位卓越的歌手,还是一位不断突破自我的创作天才。她的每一次转型都掀起新的热潮,从乡村到流行,毫无违和感🎸。\n\n🌟她的歌词总是充满故事性,让人听了忍不住细细品味✍️。无论是爱情还是友谊,她的作品都能引发共鸣,仿佛在诉说我们的故事。\n\n💖舞台上的她光芒四射,舞台下,她也在用行动支持公益事业,传递正能量。她的每一次发声都充满了力量和温暖🌈。\n\n🤔你最喜欢霉霉哪一张专辑呢?欢迎留言分享你的听歌心情,也可以推荐其他你喜欢的音乐人哦🎧!\n\n标签:#霉霉 #TaylorSwift #音乐故事 #流行巨星 #情感共鸣"},
65
+ {"role": "user", "content": artist_name}
66
+ ],
67
+ temperature=1,
68
+ max_tokens=2048,
69
+ top_p=1
70
+ )
71
+ post_text=response.choices[0].message.content
72
+ print(post_text)
73
+ response = client.chat.completions.create(
74
+ model="gpt-4o",
75
+ messages=[
76
+ {
77
+ "role": "system",
78
+ "content": "You are a helpful prompt generator. When given an artist's name, output a single-scene video prompt.\n\nThe video follows the artist in a music-related activity in their genre. Use simple, direct language, don't expose the artist's name in the prompt."
79
+ },
80
+ {
81
+ "role": "user",
82
+ "content": "Ludwig van Beethoven"
83
+ },
84
+ {
85
+ "role": "assistant",
86
+ "content": "a man of intense focus, is seated at the piano, slightly moving with the music, his hands dancing on the keys. The surrounding curtains, rich and opulent."
87
+ },
88
+ {
89
+ "role": "user",
90
+ "content": "Paul McCartney"
91
+ },
92
+ {
93
+ "role": "assistant",
94
+ "content": "A young man with shoulder-length black hair, wearing a stylish black outfit, playing an acoustic guitar on a dimly lit stage. His full face is visible, showing a calm and focused expression as he strums the guitar. A microphone stand is positioned near him, and a music stand with sheet music is in front of him. "
95
+ },
96
+ {
97
+ "role": "user",
98
+ "content": artist_name
99
+ }
100
+ ],
101
+ temperature=1,
102
+ max_tokens=150,
103
+ top_p=1
104
+ )
105
+ prompt=response.choices[0].message.content
106
+ print(prompt)
107
+ return post_text, prompt
108
+
109
+
110
+ # Call the ComfyUI workflow set up on HuggingFace Space(https://huggingface.co/spaces/yijin928/Test).
111
+ # 1. a prompt for video generation
112
+ # 2. an image for face swap
113
+ # 3. number of frames (affects the process time, the GPU usage contraint is 25 mins.)
114
+
115
+ def generate_video(prompt, num_of_frames, link):
116
+ client = Client("yijin928/Test", hf_token=huggingface_api_key)
117
+ result = client.predict(
118
+ positive_prompt=prompt,
119
+ num_frames=num_of_frames,
120
+ input_image=handle_file(link),
121
+ api_name="/generate_video",
122
+ )
123
+ print(result)
124
+
125
+
126
+ # Extract paths
127
+ video_path = result[0]["video"]
128
+ thumbnail_path = result[1]
129
+
130
+ output_dir = "/mnt/mediaefs"
131
+ os.makedirs(output_dir, exist_ok=True)
132
+
133
+ shutil.copy(video_path, os.path.join(output_dir, "output_video.mp4"))
134
+ shutil.copy(thumbnail_path, os.path.join(output_dir, "output_preview.png"))
135
+
136
+ print("Output saved to /mnt/mediaefs")
137
+ return result
138
+
139
+ if __name__ == "__main__":
140
+ parser = argparse.ArgumentParser(description="Generate Xiaohongshu-style post and video for a music artist.")
141
+ parser.add_argument("--frames", type=int, default=20, help="Number of video frames to generate")
142
+ args = parser.parse_args()
143
+
144
+ link,artist=faces_gdrive_to_hf()
145
+ print(artist,link)
146
+ post_text, prompt = generate_post_and_prompt(artist)
147
+ generate_video(prompt, num_of_frames=args.frames, link=link)
148
+
149
+ with open("/mnt/mediaefs/rednote_post.txt", "w", encoding="utf-8") as f:
150
+ f.write(post_text+"\n")