1 line
9.4 KiB
JSON
1 line
9.4 KiB
JSON
{"repo": "akshatj1708/multimodal-chatbot-gemini-palm", "files": {"app.py": {"9c3c2ca1b3aca0427647804132bc0786adf19821": {"t": 1785674365.6056669, "n": 8970, "h": "81889bd46c32795b", "c": "import os\nimport requests\nimport json\nimport base64\nfrom io import BytesIO\nfrom fastapi import FastAPI, UploadFile, File, HTTPException, Request, Form\nfrom fastapi.responses import JSONResponse, HTMLResponse, FileResponse\nfrom fastapi.staticfiles import StaticFiles\nfrom fastapi.templating import Jinja2Templates\nfrom fastapi.middleware.cors import CORSMiddleware\nfrom PIL import Image, ImageOps\nimport io\nimport torch\nfrom dotenv import load_dotenv\nimport logging\nfrom typing import Optional, Dict, Any, List, Union\nfrom pathlib import Path\nimport replicate\n\n# Load environment variables\nload_dotenv()\n\n# Configure logging\nlogging.basicConfig(level=logging.INFO)\nlogger = logging.getLogger(__name__)\n\n# Initialize FastAPI\napp = FastAPI(title=\"Hybrid AI Chatbot API\")\n\n# Setup templates\ntemplates = Jinja2Templates(directory=os.path.join(os.path.dirname(__file__), \"templates\"))\n\n# API Configuration - Load from environment variables\nOPENROUTER_API_KEY = os.getenv(\"OPENROUTER_API_KEY\")\nREPLICATE_API_TOKEN = os.getenv(\"REPLICATE_API_TOKEN\")\n\n# Validate required environment variables\nif not OPENROUTER_API_KEY:\n raise ValueError(\"OPENROUTER_API_KEY environment variable not set\")\nif not REPLICATE_API_TOKEN:\n raise ValueError(\"REPLICATE_API_TOKEN environment variable not set\")\n\n# Set Replicate API token\nos.environ[\"REPLICATE_API_TOKEN\"] = REPLICATE_API_TOKEN\n\n# Function to get the absolute path\ndef get_absolute_path(relative_path):\n return os.path.join(os.path.dirname(__file__), relative_path)\n\n# Configure CORS\napp.add_middleware(\n CORSMiddleware,\n allow_origins=[\"*\"],\n allow_credentials=True,\n allow_methods=[\"*\"],\n allow_headers=[\"*\"],\n)\n\n# Mount static files\nstatic_dir = get_absolute_path(\"static\")\nos.makedirs(static_dir, exist_ok=True)\napp.mount(\"/static\", StaticFiles(directory=static_dir), name=\"static\")\n\n# Initialize models and API clients\ntry:\n logger.info(\"Initializing API clients...\")\n \n # Test OpenRouter connection\n try:\n test_response = requests.post(\n \"https://openrouter.ai/api/v1/chat/completions\",\n headers={\n \"Authorization\": f\"Bearer {OPENROUTER_API_KEY}\",\n \"Content-Type\": \"application/json\",\n },\n json={\n \"model\": \"openai/gpt-3.5-turbo\",\n \"messages\": [{\"role\": \"user\", \"content\": \"Hello, are you working?\"}]\n }\n )\n if test_response.status_code == 200:\n logger.info(\"Successfully connected to OpenRouter API\")\n else:\n logger.warning(f\"OpenRouter test request failed with status {test_response.status_code}\")\n except Exception as e:\n logger.warning(f\"OpenRouter connection test failed: {str(e)}\")\n \n # Test Replicate connection\n try:\n client = replicate.Client(api_token=REPLICATE_API_TOKEN)\n # Just test the client initialization\n logger.info(\"Replicate client initialized\")\n except Exception as e:\n logger.warning(f\"Replicate client initialization warning: {str(e)}\")\n \n logger.info(\"API clients initialized successfully\")\n \nexcept Exception as e:\n logger.error(f\"Error initializing API clients: {str(e)}\")\n logger.error(\"Please check your API keys and internet connection\")\n raise\n\n# Global chat history\nchat_histories: Dict[str, List[Dict[str, str]]] = {}\n\ndef optimize_for_performance():\n \"\"\"Optimize PyTorch for better performance on available hardware.\"\"\"\n if torch.cuda.is_available():\n torch.backends.cudnn.benchmark = True\n torch.set_float32_matmul_precision('high')\n logger.info(\"CUDA optimization enabled\")\n\n# Apply performance optimizations\noptimize_for_performance()\n\ndef process_image(image_data: bytes) -> Image.Image:\n \"\"\"Process uploaded image data.\"\"\"\n try:\n image = Image.open(io.BytesIO(image_data))\n return image\n except Exception as e:\n logger.error(f\"Error processing image: {str(e)}\")\n raise HTTPException(status_code=400, detail=\"Invalid image file\")\n\nasync def generate_text_with_openrouter(messages: List[Dict[str, str]], model: str = \"openai/gpt-3.5-turbo\") -> str:\n \"\"\"Generate text using OpenRouter API.\"\"\"\n try:\n response = requests.post(\n \"https://openrouter.ai/api/v1/chat/completions\",\n headers={\n \"Authorization\": f\"Bearer {OPENROUTER_API_KEY}\",\n \"Content-Type\": \"application/json\",\n },\n json={\n \"model\": model,\n \"messages\": messages\n }\n )\n \n if response.status_code != 200:\n error_msg = f\"OpenRouter API error: {response.text}\"\n logger.error(error_msg)\n raise HTTPException(status_code=response.status_code, detail=error_msg)\n \n return response.json()[\"choices\"][0][\"message\"][\"content\"]\n \n except Exception as e:\n logger.error(f\"Error in OpenRouter API call: {str(e)}\")\n raise HTTPException(status_code=500, detail=f\"Error generating text: {str(e)}\")\n\nasync def generate_image_with_replicate(prompt: str) -> str:\n \"\"\"Generate image using Replicate API.\"\"\"\n try:\n output = replicate.run(\n \"stability-ai/sdxl:39ed52f2a78e934b3ba6e2a89f5b1c712de7dfea535525255b1aa35c5565e08b\",\n input={\"prompt\": prompt}\n )\n return output[0] # Returns the URL of the generated image\n \n except Exception as e:\n logger.error(f\"Error in Replicate API call: {str(e)}\")\n raise HTTPException(status_code=500, detail=f\"Error generating image: {str(e)}\")\n\n@app.post(\"/chat\")\nasync def chat(\n message: str = Form(...),\n chat_id: str = Form(\"default\"),\n image: Optional[UploadFile] = File(None)\n):\n \"\"\"Handle chat messages with optional image input using hybrid models.\"\"\"\n try:\n # Initialize chat history if it doesn't exist\n if chat_id not in chat_histories:\n chat_histories[chat_id] = []\n \n chat_history = chat_histories[chat_id]\n \n # Add user message to history\n chat_history.append({\"role\": \"user\", \"content\": message})\n \n if image:\n try:\n # Process image with Replicate\n logger.info(\"Processing image with Replicate\")\n image_data = await image.read()\n \n # For now, we'll just generate an image based on the text prompt\n # In a real implementation, you might want to use a vision model first\n image_url = await generate_image_with_replicate(message)\n \n # Add image URL to the response\n response_content = f\"Generated image based on your request: {image_url}\"\n \n # Add assistant response to history\n chat_history.append({\"role\": \"assistant\", \"content\": response_content})\n \n return JSONResponse({\n \"response\": response_content,\n \"image_url\": image_url,\n \"chat_id\": chat_id,\n \"model_used\": \"replicate\"\n })\n \n except Exception as e:\n logger.error(f\"Error processing image: {str(e)}\")\n raise HTTPException(\n status_code=400, \n detail=f\"Error processing image: {str(e)}\"\n )\n else:\n # Use OpenRouter for text generation\n logger.info(\"Using OpenRouter for text generation\")\n \n # Get response from OpenRouter\n response_text = await generate_text_with_openrouter(chat_history)\n \n # Add assistant response to history\n chat_history.append({\"role\": \"assistant\", \"content\": response_text})\n \n return JSONResponse({\n \"response\": response_text,\n \"chat_id\": chat_id,\n \"model_used\": \"openrouter\"\n })\n \n except HTTPException:\n # Re-raise HTTP exceptions as they are\n raise\n \n except Exception as e:\n logger.error(f\"Error in chat: {str(e)}\")\n raise HTTPException(\n status_code=500, \n detail=f\"An error occurred while processing your request: {str(e)}\"\n )\n\n@app.get(\"/health\")\nasync def health_check():\n \"\"\"Health check endpoint.\"\"\"\n return {\"status\": \"healthy\", \"device\": str(device)}\n\n@app.get(\"/\", response_class=HTMLResponse)\nasync def read_root(request: Request):\n return templates.TemplateResponse(\"index.html\", {\"request\": request, \"url_for\": request.url_for})\n\nif __name__ == \"__main__\":\n import uvicorn\n # Create static and templates directories if they don't exist\n Path(\"static\").mkdir(exist_ok=True)\n Path(\"templates\").mkdir(exist_ok=True)\n \n uvicorn.run(\"app:app\", host=\"0.0.0.0\", port=8000, reload=True)\n"}}}} |