subashpoudel commited on
Commit
708437f
·
1 Parent(s): 946d35b

Implememted Image Generation api

Browse files
__pycache__/main.cpython-312.pyc CHANGED
Binary files a/__pycache__/main.cpython-312.pyc and b/__pycache__/main.cpython-312.pyc differ
 
main.py CHANGED
@@ -5,11 +5,11 @@ from my_agent.agent import build_graph
5
  import pandas as pd
6
  from typing import Optional , List
7
  from my_agent.utils.initial_interaction import BusinessInteractionChatbot
8
- from my_agent.utils.utils import encode_image_to_base64 , generate_final_story
 
9
 
10
  import json
11
  from my_agent.utils.nodes import generate_final_story
12
- from collections import defaultdict
13
 
14
  # Store brainstorming results per thread_id
15
 
@@ -33,9 +33,6 @@ def business_chat(msg: UserMessage):
33
 
34
 
35
 
36
-
37
-
38
-
39
  @app.post("/brainstrom")
40
  def run_graph(
41
  query: List[str], # sent as JSON body
@@ -51,7 +48,7 @@ def run_graph(
51
  'topic': query,
52
  'images': image_base64_list,
53
  'latest_preferred_topics':preferred_topics,
54
- 'business_details': stored_data['business_details']
55
  },
56
  config={"configurable": {"thread_id": thread_id}})
57
  stored_data['brainstroming_response']=result
@@ -62,16 +59,24 @@ def run_graph(
62
  }
63
 
64
 
65
-
66
-
67
  @app.post("/generate-final-story")
68
- def generate_final_story_endpoint(
69
- ):
70
  final_story = generate_final_story(stored_data["brainstroming_response"])
 
71
  return {
72
  'response': final_story
73
  }
74
-
 
 
 
 
 
 
 
 
 
 
75
 
76
 
77
 
 
5
  import pandas as pd
6
  from typing import Optional , List
7
  from my_agent.utils.initial_interaction import BusinessInteractionChatbot
8
+ from my_agent.utils.utils import encode_image_to_base64 , generate_final_story, generate_image
9
+
10
 
11
  import json
12
  from my_agent.utils.nodes import generate_final_story
 
13
 
14
  # Store brainstorming results per thread_id
15
 
 
33
 
34
 
35
 
 
 
 
36
  @app.post("/brainstrom")
37
  def run_graph(
38
  query: List[str], # sent as JSON body
 
48
  'topic': query,
49
  'images': image_base64_list,
50
  'latest_preferred_topics':preferred_topics,
51
+ # 'business_details': stored_data['business_details']
52
  },
53
  config={"configurable": {"thread_id": thread_id}})
54
  stored_data['brainstroming_response']=result
 
59
  }
60
 
61
 
 
 
62
  @app.post("/generate-final-story")
63
+ def generate_final_story_endpoint():
 
64
  final_story = generate_final_story(stored_data["brainstroming_response"])
65
+ stored_data['final_story']=final_story
66
  return {
67
  'response': final_story
68
  }
69
+
70
+ @app.post("/generate-image")
71
+ def generate_image_endpoint():
72
+ image = generate_image(str(stored_data['final_story']))
73
+ stored_data['generated_image']=image
74
+ return {
75
+ 'image':image
76
+ }
77
+
78
+
79
+
80
 
81
 
82
 
my_agent/utils/__pycache__/prompts.cpython-312.pyc ADDED
Binary file (1.46 kB). View file
 
my_agent/utils/__pycache__/utils.cpython-312.pyc CHANGED
Binary files a/my_agent/utils/__pycache__/utils.cpython-312.pyc and b/my_agent/utils/__pycache__/utils.cpython-312.pyc differ
 
my_agent/utils/prompts.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ story_to_prompt = '''
2
+ You are an expert in crafting vivid, cinematic prompts for image generation models like Midjourney, DALL·E, or Stable Diffusion.
3
+
4
+ Given a paragraph describing a visual scene, rewrite it as a single, detailed sentence that captures the atmosphere, key visual elements, emotions, and composition — as if directing a high-quality cinematic image.
5
+
6
+ Example input:
7
+ "A bustling street scene sets the tone for a culinary journey, as the camera pans across the exterior of a cozy vegetarian restaurant, with the sign 'Vegetarian Delights' prominently displayed. The camera takes us inside, where the warm and inviting atmosphere is filled with the aroma of spices and the soft glow of lighting, highlighting the menu and the chefs expertly preparing a variety of vegetarian dishes. A satisfied customer takes a bite of their meal, with a look of delight on their face, as the camera zooms in on the flavors and textures of the dish, evoking a sense of joy and contentment."
8
+
9
+ Desired output:
10
+ "A cinematic street scene reveals the charming exterior of 'Vegetarian Delights,' then glides into a warmly lit, spice-scented interior where skilled chefs craft vibrant vegetarian dishes and a delighted diner savors each flavorful bite in glowing ambiance."
11
+ '''
my_agent/utils/utils.py CHANGED
@@ -6,6 +6,9 @@ import base64
6
  from PIL import Image
7
  from io import BytesIO
8
  from fastapi import UploadFile
 
 
 
9
  def generate_final_story(query):
10
  if len(query['preferred_topics'])>0:
11
  template = f'''I want to create a detailed storyline for a video in the given topic. You have to provide me that storyline what to include in the video.
@@ -39,4 +42,47 @@ def encode_image_to_base64(uploaded_file: UploadFile) -> str:
39
  # Convert base64 string to PIL image (optional for LangGraph processing)
40
  def process_image(base64_str: str) -> Image.Image:
41
  image_data = base64.b64decode(base64_str)
42
- return Image.open(BytesIO(image_data))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  from PIL import Image
7
  from io import BytesIO
8
  from fastapi import UploadFile
9
+ from huggingface_hub import InferenceClient
10
+ from .prompts import story_to_prompt
11
+
12
  def generate_final_story(query):
13
  if len(query['preferred_topics'])>0:
14
  template = f'''I want to create a detailed storyline for a video in the given topic. You have to provide me that storyline what to include in the video.
 
42
  # Convert base64 string to PIL image (optional for LangGraph processing)
43
  def process_image(base64_str: str) -> Image.Image:
44
  image_data = base64.b64decode(base64_str)
45
+ return Image.open(BytesIO(image_data))
46
+
47
+
48
+ def generate_prompt(final_story):
49
+ print('************Entering prompt generator****************')
50
+ messages = [
51
+ (
52
+ "system",
53
+ story_to_prompt,
54
+ ),
55
+ ("human", final_story),
56
+ ]
57
+ prompt = llm.invoke(messages)
58
+ print('The prompt is:',prompt)
59
+ return prompt.content
60
+
61
+ def generate_image(final_story):
62
+ prompt = generate_prompt(final_story)
63
+ print('************Finished prompt generator****************')
64
+
65
+ client = InferenceClient(
66
+ provider="hf-inference",
67
+ api_key="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",
68
+ )
69
+
70
+ print('************Finished calling generator****************')
71
+
72
+
73
+ # output is a PIL.Image object
74
+ image = client.text_to_image(
75
+ prompt,
76
+ model="black-forest-labs/FLUX.1-schnell",
77
+ )
78
+ print('*****************Image Created*******************')
79
+
80
+ image.save('image.png')
81
+ print('*****************Image Saved*******************')
82
+ return "Image Created"
83
+ # try:
84
+ # return image
85
+ # except:
86
+ # return 'Image created'
87
+
88
+