Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI , UploadFile , File , Form | |
| from fastapi.responses import JSONResponse | |
| from pydantic import BaseModel | |
| from my_agent.agent import build_graph | |
| import pandas as pd | |
| from typing import Optional , List | |
| from my_agent.utils.initial_interaction import BusinessInteractionChatbot | |
| from my_agent.utils.utils import encode_image_to_base64 , generate_final_story, generate_image | |
| import json | |
| # Store brainstorming results per thread_id | |
| app = FastAPI() | |
| interaction_chatbot = BusinessInteractionChatbot() | |
| graph = build_graph() | |
| stored_data={} | |
| class UserMessage(BaseModel): | |
| message: str | |
| def business_chat(msg: UserMessage): | |
| response = interaction_chatbot.chat(msg.message) | |
| if interaction_chatbot.is_complete(response): | |
| details = interaction_chatbot.extract_details() | |
| stored_data['business_details'] = details | |
| return {"response": response, "business_details": details, "complete": True} | |
| return {"response": response, "complete": False} | |
| def run_graph( | |
| query: List[str], # sent as JSON body | |
| preferred_topics: Optional[list] = [], | |
| images: Optional[List[UploadFile]] = [], # ✅ Optional UploadFile list | |
| thread_id: Optional[str] = "default-session", | |
| ): | |
| # Convert uploaded images to base64 | |
| image_base64_list = [encode_image_to_base64(img) for img in images] | |
| # Invoke LangGraph | |
| result = graph.invoke({ | |
| 'topic': query, | |
| 'images': image_base64_list, | |
| 'latest_preferred_topics':preferred_topics, | |
| # 'business_details': stored_data['business_details'] | |
| }, | |
| config={"configurable": {"thread_id": thread_id}}) | |
| stored_data['brainstroming_response']=result | |
| # brainstorm_store[thread_id] = result | |
| return { | |
| 'response': result, | |
| } | |
| def generate_final_story_endpoint(): | |
| final_story = generate_final_story(stored_data["brainstroming_response"]) | |
| stored_data['final_story']=final_story | |
| return { | |
| 'response': final_story | |
| } | |
| def generate_image_endpoint(): | |
| image = generate_image(str(stored_data['final_story'])) | |
| stored_data['generated_image']=image | |
| return { | |
| 'response':image | |
| } | |