File size: 7,980 Bytes
a9f99c3
 
be3a5c4
583f6dd
be3a5c4
a9f99c3
583f6dd
 
 
24b940c
583f6dd
 
 
 
 
a6ebaaf
583f6dd
6874dac
a9f99c3
946d35b
 
be3a5c4
 
93a5bf9
 
eb40d68
93a5bf9
9acd478
fa8520f
be3a5c4
b623e6c
fa8520f
b55b8d4
fa8520f
3c1150c
 
fa8520f
 
 
6874dac
 
 
 
 
 
 
 
 
 
 
 
 
 
3c1150c
fa8520f
 
93a5bf9
 
 
 
 
db141d0
 
24b940c
 
 
 
3c1150c
946d35b
fbc17f4
b55b8d4
 
85a68fb
6874dac
 
 
 
508df21
 
93a5bf9
 
db141d0
 
92115be
 
eb40d68
 
 
 
9acd478
b623e6c
a6ebaaf
 
 
6874dac
 
a6ebaaf
 
 
c636895
b623e6c
 
fbc17f4
a6ebaaf
 
fbc17f4
a6ebaaf
9acd478
 
 
 
 
 
 
 
 
 
 
a0929ab
fbc17f4
9acd478
 
 
a0929ab
9acd478
 
 
eb40d68
b623e6c
 
 
 
eb40d68
 
 
c636895
 
eb40d68
85a68fb
3c1150c
da1776b
eb40d68
a9f99c3
fbc17f4
 
 
 
 
 
 
93a5bf9
fbc17f4
 
eb40d68
 
6874dac
a9f99c3
eb40d68
 
 
 
fbc17f4
 
 
 
eb40d68
85a68fb
946d35b
 
708437f
6874dac
 
 
708437f
946d35b
 
 
b4fb6ac
6874dac
 
 
3c1150c
 
708437f
 
fbc17f4
6874dac
fbc17f4
708437f
 
06e8ef4
708437f
 
 
 
946d35b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from fastapi import FastAPI , UploadFile , File , Form 
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from src.genai.brainstroming_agent.agent import brainstroming_graph
import pandas as pd
from typing import Optional , List
from src.genai.context_analysis_agent.agent import IntroductionChatbot
from src.genai.business_interaction_agent.agent import BusinessInteractionChatbot
from src.genai.context_analysis_agent.utils.utils import save_to_db
import ast
from src.genai.orchestration_agent.agent import orchestration_chat
from src.genai.orchestration_agent.utils.utils import caption_image , show_analytics
from src.genai.brainstroming_agent.utils.utils import encode_image_to_base64 , generate_final_story, generate_image
from src.genai.idea_to_budget_agent.agent import budget_calculator
from src.genai.ideation_agent.agent import ideation_graph
from langgraph.errors import GraphRecursionError
from src.genai.human_refined_ideation.agent import human_refined_idea
from dummy_state import stored_data, long_term_memory
import json

# Store brainstorming results per thread_id

app = FastAPI()
context_analysis_graph = IntroductionChatbot()
business_interaction_graph = BusinessInteractionChatbot()
idea_graph = ideation_graph()
brainstrom_graph = brainstroming_graph()
human_refine_graph = human_refined_idea()
# orchestrate_graph = orchestration_chat()


class OrchestrationRequest(BaseModel):
    message: str
    image_base64 : Optional[list] = []

@app.post("/orchestration")
def orchestration_endpoint(request:OrchestrationRequest):
    print('Image:',request.image_base64)
    result = orchestration_chat(request.message , request.image_base64)
    if result.image_caption != '':
        stored_data['image_caption']=result.image_caption
    if result.video_idea !='' or result.video_idea != 'null':
        stored_data['refined_ideation']= result.video_idea
    if result.video_story!='' or result.video_story!='null':
        stored_data['final_story']= result.video_story
    print('Idea:',stored_data['refined_ideation'])
    print('Story:', stored_data['final_story'])

    return {'tool_response': result.tool ,
             'message_response': result.query_response, 
             'image_caption':result.image_caption,
             'video_idea': result.video_idea,
             'video_story': result.video_story}

class UserMessage(BaseModel):
    message: str
@app.post("/context-analysis")
def context_analysis(msg: UserMessage):
    response = context_analysis_graph.chat(msg.message)
    if context_analysis_graph.is_complete(response):
        details = context_analysis_graph.extract_details()
        if type(details) != dict:
            details = details.model_dump()
        print('Business_details:',details)
        if isinstance(details, str):
            details= ast.literal_eval(details)
        print('Details Type:',type(details))
        # save_to_db(details)
        stored_data['business_details'] = details
        context_analysis_graph.reset()
        return {"response": response, "business_details": details, "complete": True}
    return {"response": response, "complete": False}

@app.post("/show-analytics")
def show_analytics_endpoint():
    response = show_analytics(stored_data['business_details'])
    return {'response': response}


@app.post("/business-interaction")
def business_interaction(interaction: str):
    response,business_details = business_interaction_graph.chat(interaction , stored_data['business_details'])
    stored_data['business_details']=business_details
    return {'response': response}

class IdeationRequest(BaseModel):
    topic : List[str]

@app.post("/ideation")
def ideation_endpoint():
    config={"recursion_limit":15, "configurable": {"thread_id": "ideation_thread123"}}
    try:
        result = idea_graph.invoke(
        {
            'business_details': [stored_data['business_details']],
            'image_caption': [stored_data['image_caption'] if 'image_caption' in stored_data else ""]
        },
        config=config,
        )
        stored_data['final_ideation'] = result['improver_response'][-1]
        stored_data['final_ideation']=ast.literal_eval(stored_data['final_ideation'])

        return {'response':ast.literal_eval(result['improver_response'][-1])}
    except GraphRecursionError:
        result = idea_graph.get_state({"configurable": {"thread_id": "ideation_thread123"}})
        return {'response': ast.literal_eval(result[0])}
    
class RefineIdeationRequest(BaseModel):
    query: str
    thread_id: Optional[str]="refine_ideas_thread"


@app.post("/human-idea-refining")
def human_idea_refine_endpoint(request:RefineIdeationRequest):
    stored_data['human_ideation_interactions'].append({"role": "user", "content": request.query})
    response = human_refine_graph.invoke(
        {
        'query': stored_data['human_ideation_interactions'],
        'business_details': stored_data["business_details"],
        'final_ideation': stored_data.get('final_ideation',["","","",""]),
        },config={"configurable": {"thread_id": request.thread_id}}
    )
    stored_data['human_ideation_interactions'].append({"role": "assistant", "content": response['result']})
    stored_data['refined_ideation'] = stored_data['human_ideation_interactions'][-1]['content']
    return {'response' : stored_data['human_ideation_interactions'][-1]['content'] }



@app.post("/budget-mapping")
def budget_mapping_endpoint():
    result = budget_calculator(stored_data["business_details"],stored_data['final_ideation'])
    return {'response':result}


class BrainstormRequest(BaseModel):
    preferred_topics: Optional[list] = []
    image_base64_list: Optional[list] = []
    thread_id: Optional[str]="default-session"

@app.post("/brainstorm")
def brainstroming_endpoint(
    request: BrainstormRequest,  # 🔥 Full JSON body here
):
    idea = (
    [stored_data['refined_ideation']]
    if stored_data.get('refined_ideation')
    else [str(stored_data['final_ideation'])]
    if stored_data.get('final_ideation')
    else ['''I don't have any idea right now. Create your own **very creative** and **out of the box** video idea and generate the story for now.'''])

    result = brainstrom_graph.invoke({
        # 'idea': [stored_data.get('refined_ideation', 'final_ideation', )],
        'idea': idea,
        'images': request.image_base64_list,
        'latest_preferred_topics': request.preferred_topics,
        'business_details': stored_data['business_details']
    },
    config={"configurable": {"thread_id": request.thread_id}})
    
    stored_data['brainstroming_response'] = result

    return {'response':{
        "story": result['stories'][-1],
        "brainstorming_topics": result['brainstroming_topics'][-1]
    }}



@app.post("/generate-final-story")
def generate_final_story_endpoint():
    final_story = generate_final_story(
    stored_data.get("brainstroming_response") or stored_data.get("business_details")
    )
    stored_data['final_story']=final_story
    return {
        'response': final_story
    }

# stored_data['final_story']= '''A cinematic journey follows a street magician\'s 
# metamorphosis from a mere trickster to a powerful performer, as he transforms his act with newfound physical strength, effortlessly executing death-defying stunts, and inspiring a captivated crowd to take action, all set against a 
# backdrop of urban grandeur and pulsing energy.'''


@app.post("/generate-image")  
def generate_image_endpoint():
    image = generate_image(str(stored_data.get('final_story','''I don't have any story right now. Just use the business details for now.'''))
                           ,str(stored_data.get('business_details'))
                           ,str(stored_data.get('refined_ideation','''I don't have any idea right now. Just use the business details for now.''')))
    stored_data['generated_image']=image
    return {
        'response':image
    }