hmb HF Staff commited on
Commit
8ececae
·
verified ·
1 Parent(s): 6a775f8

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +48 -33
  2. workflow.json +31 -112
app.py CHANGED
@@ -1,54 +1,69 @@
1
- import gradio as gr
2
 
 
 
3
 
4
- def _qwen(system: str, user: str) -> str:
5
- return (
6
- f"<|im_start|>system\n{system}<|im_end|>\n"
7
- f"<|im_start|>user\n{user}<|im_end|>\n"
8
- "<|im_start|>assistant\n"
9
- )
10
 
11
 
12
- def scene_prompt_input(moment: str) -> str:
13
- return _qwen(
14
- "You write precise FLUX image generation prompts. Output ONE sentence only, "
15
- "starting with 'World Cup 2026 stadium,'",
16
- f"Write an image generation prompt for this match moment: {moment}",
 
 
 
17
  )
 
 
18
 
 
 
 
 
 
19
 
20
- def tweet_input(moment: str) -> str:
21
- return _qwen(
22
  "You write punchy viral sports tweets. Max 240 chars. 2-3 emojis. "
23
- "End with #WorldCup2026. Output only the tweet text, nothing else.",
24
- f"Write a tweet about this World Cup moment: {moment}",
 
25
  )
26
 
27
-
28
- def instagram_input(moment: str) -> str:
29
- return _qwen(
30
- "You write engaging Instagram captions for sports moments. "
31
- "3-5 sentences, storytelling tone, end with 10 relevant hashtags.",
32
- f"Write an Instagram caption about this World Cup moment: {moment}",
 
33
  )
34
 
 
 
 
 
 
 
 
 
35
 
36
- def linkedin_input(moment: str) -> str:
37
- return _qwen(
38
- "You write professional LinkedIn posts about sports as a lens for leadership "
39
- "and teamwork. 150-200 words, compelling hook.",
40
- f"Write a LinkedIn post inspired by this World Cup moment: {moment}",
 
41
  )
42
 
 
 
43
 
44
  demo = gr.Workflow(
45
  graph="workflow.json",
46
- bind={
47
- "Scene Prompt Input": scene_prompt_input,
48
- "Tweet Input": tweet_input,
49
- "Instagram Input": instagram_input,
50
- "LinkedIn Input": linkedin_input,
51
- },
52
  )
53
 
54
  if __name__ == "__main__":
 
1
+ import os
2
 
3
+ import gradio as gr
4
+ from huggingface_hub import InferenceClient
5
 
6
+ TEXT_MODEL = "Qwen/Qwen2.5-72B-Instruct"
 
 
 
 
 
7
 
8
 
9
+ def _chat(client: InferenceClient, system: str, user: str, max_tokens: int = 300) -> str:
10
+ resp = client.chat.completions.create(
11
+ model=TEXT_MODEL,
12
+ messages=[
13
+ {"role": "system", "content": system},
14
+ {"role": "user", "content": user},
15
+ ],
16
+ max_tokens=max_tokens,
17
  )
18
+ return (resp.choices[0].message.content or "").strip()
19
+
20
 
21
+ def create_content(
22
+ moment: str, oauth_token: gr.OAuthToken | None = None
23
+ ) -> tuple[str, str, str, str]:
24
+ token = (oauth_token.token if oauth_token else None) or os.environ.get("HF_TOKEN")
25
+ client = InferenceClient(token=token)
26
 
27
+ tweet = _chat(
28
+ client,
29
  "You write punchy viral sports tweets. Max 240 chars. 2-3 emojis. "
30
+ "End with #WorldCup2026. Output ONLY the tweet, no commentary.",
31
+ f"World Cup moment: {moment}",
32
+ max_tokens=120,
33
  )
34
 
35
+ instagram = _chat(
36
+ client,
37
+ "You write high-engagement Instagram sports captions. "
38
+ "Open with a punchy hook. 3-4 vivid sentences building the story. "
39
+ "End with 12-15 hashtags on a new line.",
40
+ f"World Cup moment: {moment}",
41
+ max_tokens=350,
42
  )
43
 
44
+ linkedin = _chat(
45
+ client,
46
+ "You write professional LinkedIn posts that use a sports moment as a lens for "
47
+ "leadership or teamwork. Bold opening statement. 150-200 words. No hashtags. "
48
+ "End with a question to drive comments.",
49
+ f"World Cup moment: {moment}",
50
+ max_tokens=450,
51
+ )
52
 
53
+ scene_prompt = _chat(
54
+ client,
55
+ "Write a single FLUX image editing prompt starting with "
56
+ "'Transform into a cinematic World Cup 2026 poster,' — one sentence only.",
57
+ f"Match moment: {moment}",
58
+ max_tokens=60,
59
  )
60
 
61
+ return tweet, instagram, linkedin, scene_prompt
62
+
63
 
64
  demo = gr.Workflow(
65
  graph="workflow.json",
66
+ bind={"Content Creator": create_content},
 
 
 
 
 
67
  )
68
 
69
  if __name__ == "__main__":
workflow.json CHANGED
@@ -21,33 +21,28 @@
21
  "asset_type": "text",
22
  "inputs": [{ "id": "in", "label": "Text", "type": "text" }],
23
  "outputs": [{ "id": "out", "label": "Text", "type": "text" }],
24
- "x": 80, "y": 300, "width": 220, "height": 161,
25
  "data": { "in": "USA score in 94th minute to beat Mexico 2-1 in the group stage, LA crowd erupts" }
26
  }
27
  ],
28
 
29
  "operators": [
30
  {
31
- "id": "op_scene_fn",
32
- "label": "Scene Prompt Input",
33
  "role": "operator",
34
  "kind": "fn",
35
- "fn": "Scene Prompt Input",
36
- "inputs": [{ "id": "in_moment", "label": "moment", "type": "text", "required": true }],
37
- "outputs": [{ "id": "out_0", "label": "output", "type": "text", "output_index": 0 }],
38
- "x": 380, "y": 80, "width": 220, "height": 121,
39
- "data": {}
40
- },
41
- {
42
- "id": "op_scene_model",
43
- "label": "Generate Scene Prompt",
44
- "role": "operator",
45
- "kind": "model",
46
- "model_id": "Qwen/Qwen2.5-72B-Instruct",
47
- "pipeline_tag": "text-generation",
48
- "inputs": [{ "id": "in_0", "label": "inputs", "type": "text", "required": true }],
49
- "outputs": [{ "id": "out_0", "label": "output", "type": "text", "output_index": 0 }],
50
- "x": 650, "y": 80, "width": 220, "height": 121,
51
  "data": {}
52
  },
53
  {
@@ -59,81 +54,12 @@
59
  "endpoint": "/infer",
60
  "inputs": [
61
  { "id": "in_image", "label": "input_image", "type": "image", "required": true },
62
- { "id": "in_prompt", "label": "prompt", "type": "text", "required": true }
63
  ],
64
  "outputs": [
65
  { "id": "out_0", "label": "Result", "type": "image", "output_index": 0 }
66
  ],
67
- "x": 920, "y": 80, "width": 220, "height": 121,
68
- "data": {}
69
- },
70
- {
71
- "id": "op_tweet_fn",
72
- "label": "Tweet Input",
73
- "role": "operator",
74
- "kind": "fn",
75
- "fn": "Tweet Input",
76
- "inputs": [{ "id": "in_moment", "label": "moment", "type": "text", "required": true }],
77
- "outputs": [{ "id": "out_0", "label": "output", "type": "text", "output_index": 0 }],
78
- "x": 380, "y": 260, "width": 220, "height": 121,
79
- "data": {}
80
- },
81
- {
82
- "id": "op_tweet_model",
83
- "label": "Generate Tweet",
84
- "role": "operator",
85
- "kind": "model",
86
- "model_id": "Qwen/Qwen2.5-72B-Instruct",
87
- "pipeline_tag": "text-generation",
88
- "inputs": [{ "id": "in_0", "label": "inputs", "type": "text", "required": true }],
89
- "outputs": [{ "id": "out_0", "label": "output", "type": "text", "output_index": 0 }],
90
- "x": 650, "y": 260, "width": 220, "height": 121,
91
- "data": {}
92
- },
93
- {
94
- "id": "op_ig_fn",
95
- "label": "Instagram Input",
96
- "role": "operator",
97
- "kind": "fn",
98
- "fn": "Instagram Input",
99
- "inputs": [{ "id": "in_moment", "label": "moment", "type": "text", "required": true }],
100
- "outputs": [{ "id": "out_0", "label": "output", "type": "text", "output_index": 0 }],
101
- "x": 380, "y": 420, "width": 220, "height": 121,
102
- "data": {}
103
- },
104
- {
105
- "id": "op_ig_model",
106
- "label": "Generate Instagram Caption",
107
- "role": "operator",
108
- "kind": "model",
109
- "model_id": "Qwen/Qwen2.5-72B-Instruct",
110
- "pipeline_tag": "text-generation",
111
- "inputs": [{ "id": "in_0", "label": "inputs", "type": "text", "required": true }],
112
- "outputs": [{ "id": "out_0", "label": "output", "type": "text", "output_index": 0 }],
113
- "x": 650, "y": 420, "width": 220, "height": 121,
114
- "data": {}
115
- },
116
- {
117
- "id": "op_linkedin_fn",
118
- "label": "LinkedIn Input",
119
- "role": "operator",
120
- "kind": "fn",
121
- "fn": "LinkedIn Input",
122
- "inputs": [{ "id": "in_moment", "label": "moment", "type": "text", "required": true }],
123
- "outputs": [{ "id": "out_0", "label": "output", "type": "text", "output_index": 0 }],
124
- "x": 380, "y": 580, "width": 220, "height": 121,
125
- "data": {}
126
- },
127
- {
128
- "id": "op_linkedin_model",
129
- "label": "Generate LinkedIn Post",
130
- "role": "operator",
131
- "kind": "model",
132
- "model_id": "Qwen/Qwen2.5-72B-Instruct",
133
- "pipeline_tag": "text-generation",
134
- "inputs": [{ "id": "in_0", "label": "inputs", "type": "text", "required": true }],
135
- "outputs": [{ "id": "out_0", "label": "output", "type": "text", "output_index": 0 }],
136
- "x": 650, "y": 580, "width": 220, "height": 121,
137
  "data": {}
138
  }
139
  ],
@@ -144,9 +70,9 @@
144
  "label": "Match Scene",
145
  "role": "subject",
146
  "asset_type": "image",
147
- "inputs": [{ "id": "in", "label": "Image", "type": "image" }],
148
  "outputs": [{ "id": "out", "label": "Image", "type": "image" }],
149
- "x": 1190, "y": 80, "width": 220, "height": 107,
150
  "data": {}
151
  },
152
  {
@@ -154,9 +80,9 @@
154
  "label": "Tweet",
155
  "role": "subject",
156
  "asset_type": "text",
157
- "inputs": [{ "id": "in", "label": "Text", "type": "text" }],
158
  "outputs": [{ "id": "out", "label": "Text", "type": "text" }],
159
- "x": 920, "y": 260, "width": 220, "height": 107,
160
  "data": {}
161
  },
162
  {
@@ -164,9 +90,9 @@
164
  "label": "Instagram Caption",
165
  "role": "subject",
166
  "asset_type": "text",
167
- "inputs": [{ "id": "in", "label": "Text", "type": "text" }],
168
  "outputs": [{ "id": "out", "label": "Text", "type": "text" }],
169
- "x": 920, "y": 420, "width": 220, "height": 107,
170
  "data": {}
171
  },
172
  {
@@ -174,28 +100,21 @@
174
  "label": "LinkedIn Post",
175
  "role": "subject",
176
  "asset_type": "text",
177
- "inputs": [{ "id": "in", "label": "Text", "type": "text" }],
178
  "outputs": [{ "id": "out", "label": "Text", "type": "text" }],
179
- "x": 920, "y": 580, "width": 220, "height": 107,
180
  "data": {}
181
  }
182
  ],
183
 
184
  "edges": [
185
- { "id": "e1", "from_node_id": "ref_moment", "from_port_id": "out", "to_node_id": "op_scene_fn", "to_port_id": "in_moment", "type": "text" },
186
- { "id": "e2", "from_node_id": "op_scene_fn", "from_port_id": "out_0", "to_node_id": "op_scene_model", "to_port_id": "in_0", "type": "text" },
187
- { "id": "e3", "from_node_id": "op_scene_model", "from_port_id": "out_0", "to_node_id": "op_flux", "to_port_id": "in_prompt", "type": "text" },
188
- { "id": "e4", "from_node_id": "ref_image", "from_port_id": "out", "to_node_id": "op_flux", "to_port_id": "in_image", "type": "image" },
189
- { "id": "e5", "from_node_id": "op_flux", "from_port_id": "out_0", "to_node_id": "sub_image", "to_port_id": "in", "type": "image" },
190
- { "id": "e6", "from_node_id": "ref_moment", "from_port_id": "out", "to_node_id": "op_tweet_fn", "to_port_id": "in_moment", "type": "text" },
191
- { "id": "e7", "from_node_id": "op_tweet_fn", "from_port_id": "out_0", "to_node_id": "op_tweet_model", "to_port_id": "in_0", "type": "text" },
192
- { "id": "e8", "from_node_id": "op_tweet_model", "from_port_id": "out_0", "to_node_id": "sub_tweet", "to_port_id": "in", "type": "text" },
193
- { "id": "e9", "from_node_id": "ref_moment", "from_port_id": "out", "to_node_id": "op_ig_fn", "to_port_id": "in_moment", "type": "text" },
194
- { "id": "e10", "from_node_id": "op_ig_fn", "from_port_id": "out_0", "to_node_id": "op_ig_model", "to_port_id": "in_0", "type": "text" },
195
- { "id": "e11", "from_node_id": "op_ig_model", "from_port_id": "out_0", "to_node_id": "sub_instagram", "to_port_id": "in", "type": "text" },
196
- { "id": "e12", "from_node_id": "ref_moment", "from_port_id": "out", "to_node_id": "op_linkedin_fn", "to_port_id": "in_moment", "type": "text" },
197
- { "id": "e13", "from_node_id": "op_linkedin_fn", "from_port_id": "out_0", "to_node_id": "op_linkedin_model","to_port_id": "in_0", "type": "text" },
198
- { "id": "e14", "from_node_id": "op_linkedin_model","from_port_id": "out_0", "to_node_id": "sub_linkedin", "to_port_id": "in", "type": "text" }
199
  ],
200
 
201
  "view": { "default": "canvas" }
 
21
  "asset_type": "text",
22
  "inputs": [{ "id": "in", "label": "Text", "type": "text" }],
23
  "outputs": [{ "id": "out", "label": "Text", "type": "text" }],
24
+ "x": 80, "y": 280, "width": 220, "height": 200,
25
  "data": { "in": "USA score in 94th minute to beat Mexico 2-1 in the group stage, LA crowd erupts" }
26
  }
27
  ],
28
 
29
  "operators": [
30
  {
31
+ "id": "op_content",
32
+ "label": "Content Creator",
33
  "role": "operator",
34
  "kind": "fn",
35
+ "fn": "Content Creator",
36
+ "inputs": [
37
+ { "id": "in_moment", "label": "moment", "type": "text", "required": true }
38
+ ],
39
+ "outputs": [
40
+ { "id": "out_tweet", "label": "tweet", "type": "text", "output_index": 0 },
41
+ { "id": "out_ig", "label": "instagram", "type": "text", "output_index": 1 },
42
+ { "id": "out_linkedin", "label": "linkedin", "type": "text", "output_index": 2 },
43
+ { "id": "out_scene", "label": "scene_prompt", "type": "text", "output_index": 3 }
44
+ ],
45
+ "x": 380, "y": 280, "width": 240, "height": 200,
 
 
 
 
 
46
  "data": {}
47
  },
48
  {
 
54
  "endpoint": "/infer",
55
  "inputs": [
56
  { "id": "in_image", "label": "input_image", "type": "image", "required": true },
57
+ { "id": "in_prompt", "label": "prompt", "type": "text", "required": true }
58
  ],
59
  "outputs": [
60
  { "id": "out_0", "label": "Result", "type": "image", "output_index": 0 }
61
  ],
62
+ "x": 700, "y": 80, "width": 220, "height": 121,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  "data": {}
64
  }
65
  ],
 
70
  "label": "Match Scene",
71
  "role": "subject",
72
  "asset_type": "image",
73
+ "inputs": [{ "id": "in", "label": "Image", "type": "image" }],
74
  "outputs": [{ "id": "out", "label": "Image", "type": "image" }],
75
+ "x": 980, "y": 80, "width": 220, "height": 107,
76
  "data": {}
77
  },
78
  {
 
80
  "label": "Tweet",
81
  "role": "subject",
82
  "asset_type": "text",
83
+ "inputs": [{ "id": "in", "label": "Text", "type": "text" }],
84
  "outputs": [{ "id": "out", "label": "Text", "type": "text" }],
85
+ "x": 700, "y": 260, "width": 220, "height": 107,
86
  "data": {}
87
  },
88
  {
 
90
  "label": "Instagram Caption",
91
  "role": "subject",
92
  "asset_type": "text",
93
+ "inputs": [{ "id": "in", "label": "Text", "type": "text" }],
94
  "outputs": [{ "id": "out", "label": "Text", "type": "text" }],
95
+ "x": 700, "y": 400, "width": 220, "height": 107,
96
  "data": {}
97
  },
98
  {
 
100
  "label": "LinkedIn Post",
101
  "role": "subject",
102
  "asset_type": "text",
103
+ "inputs": [{ "id": "in", "label": "Text", "type": "text" }],
104
  "outputs": [{ "id": "out", "label": "Text", "type": "text" }],
105
+ "x": 700, "y": 540, "width": 220, "height": 107,
106
  "data": {}
107
  }
108
  ],
109
 
110
  "edges": [
111
+ { "id": "e1", "from_node_id": "ref_moment", "from_port_id": "out", "to_node_id": "op_content", "to_port_id": "in_moment", "type": "text" },
112
+ { "id": "e2", "from_node_id": "ref_image", "from_port_id": "out", "to_node_id": "op_flux", "to_port_id": "in_image", "type": "image" },
113
+ { "id": "e3", "from_node_id": "op_content", "from_port_id": "out_scene", "to_node_id": "op_flux", "to_port_id": "in_prompt", "type": "text" },
114
+ { "id": "e4", "from_node_id": "op_flux", "from_port_id": "out_0", "to_node_id": "sub_image", "to_port_id": "in", "type": "image" },
115
+ { "id": "e5", "from_node_id": "op_content", "from_port_id": "out_tweet", "to_node_id": "sub_tweet", "to_port_id": "in", "type": "text" },
116
+ { "id": "e6", "from_node_id": "op_content", "from_port_id": "out_ig", "to_node_id": "sub_instagram","to_port_id": "in", "type": "text" },
117
+ { "id": "e7", "from_node_id": "op_content", "from_port_id": "out_linkedin","to_node_id": "sub_linkedin","to_port_id": "in", "type": "text" }
 
 
 
 
 
 
 
118
  ],
119
 
120
  "view": { "default": "canvas" }