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 IntroductionChatbot | |
| from my_agent.utils.business_interaction import BusinessInteractionChatbot | |
| # from my_agent.utils.check 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() | |
| introduction_chatbot = IntroductionChatbot() | |
| interaction_chatbot2 = BusinessInteractionChatbot() | |
| graph = build_graph() | |
| stored_data={} | |
| class UserMessage(BaseModel): | |
| message: str | |
| def business_introduction_chat(msg: UserMessage): | |
| response = introduction_chatbot.chat(msg.message) | |
| if introduction_chatbot.is_complete(response): | |
| details = introduction_chatbot.extract_details() | |
| stored_data['business_details'] = details | |
| return {"response": response, "business_details": details, "complete": True} | |
| return {"response": response, "complete": False} | |
| def business_interaction_chat(interaction: str): | |
| response = interaction_chatbot2.chat(interaction) | |
| return {'response': response} | |
| def brainstroming_endpoint( | |
| 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': (lambda d: d['business_details'] if 'business_details' in d else {})(stored_data) | |
| }, | |
| 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 | |
| } | |