3emibrahim/debugging-testing1
0
1from fastapi import FastAPI, UploadFile, File, Form, HTTPException2from fastapi.staticfiles import StaticFiles3from fastapi.responses import FileResponse, JSONResponse4import cloudinary5import cloudinary.uploader6import requests7import io8import os9import mimetypes10import tempfile11from pathlib import Path12 13from google import genai14from google.genai import types15 16# Import required libraries for file conversion17import pandas as pd18from PyPDF2 import PdfReader19import pptx20import textract21 22# Cloudinary Config23cloudinary.config(24 cloud_name="dsrjsulxu",25 api_key="494988785133644",26 api_secret="9CroDeskvcA5ZoNiNoZddHguny0"27)28 29# Google Gemini Client30genai_client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))31 32def upload_file_to_cloudinary(file_bytes: bytes, file_type: str) -> str:33 try:34 result = cloudinary.uploader.upload(35 io.BytesIO(file_bytes),36 resource_type="raw" if file_type == "text" else "auto"37 )38 return result.get("secure_url")39 except Exception as e:40 print(f"Cloudinary upload error: {e}")41 return None42 43def upload_image(file_bytes: bytes) -> str:44 try:45 result = cloudinary.uploader.upload(io.BytesIO(file_bytes))46 return result.get("secure_url")47 except Exception as e:48 print(f"Cloudinary upload error: {e}")49 return None50 51def convert_to_text(file_bytes: bytes, file_type: str, filename: str) -> str:52 """Convert various file types to plain text"""53 try:54 # Create a temporary file to process55 with tempfile.NamedTemporaryFile(delete=False, suffix=Path(filename).suffix) as temp_file:56 temp_file.write(file_bytes)57 temp_path = temp_file.name58 59 text_content = ""60 61 if file_type == "application/pdf":62 # Convert PDF to text63 pdf = PdfReader(temp_path)64 for page in pdf.pages:65 text_content += page.extract_text() + "\n"66 67 elif file_type in ["application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"]:68 # Convert Excel to text69 df = pd.read_excel(temp_path)70 text_content = df.to_string()71 72 elif file_type in ["application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.presentationml.presentation"]:73 # Convert PowerPoint to text74 ppt = pptx.Presentation(temp_path)75 for slide in ppt.slides:76 for shape in slide.shapes:77 if hasattr(shape, "text"):78 text_content += shape.text + "\n"79 text_content += "\n---\n"80 81 elif file_type == "text/plain":82 # Already text, just read it83 with open(temp_path, 'r', encoding='utf-8', errors='ignore') as f:84 text_content = f.read()85 86 else:87 # Try using textract for other types88 text_content = textract.process(temp_path).decode('utf-8', errors='ignore')89 90 # Clean up temporary file91 os.unlink(temp_path)92 93 return text_content94 95 except Exception as e:96 print(f"Conversion error: {e}")97 raise HTTPException(status_code=500, detail=f"File conversion failed: {str(e)}")98 99def generate_response(user_prompt: str, image_url: str) -> str:100 try:101 # Download image from URL102 image_bytes = requests.get(image_url).content103 104 # Prepare image part105 image_part = types.Part.from_bytes(106 data=image_bytes, mime_type="image/jpeg"107 )108 109 # Send to Gemini110 response = genai_client.models.generate_content(111 model="gemini-2.0-flash-exp",112 contents=[user_prompt, image_part],113 )114 115 return response.text116 except Exception as e:117 return f"Google Gemini API error: {e}"118 119def generate_response_for_document(user_prompt: str, doc_url: str, mime_type: str) -> str:120 try:121 # Download document from URL122 doc_data = requests.get(doc_url).content123 124 # Send to Gemini125 response = genai_client.models.generate_content(126 model="gemini-2.0-flash",127 contents=[128 types.Part.from_bytes(129 data=doc_data,130 mime_type=mime_type,131 ),132 user_prompt133 ]134 )135 136 return response.text137 except Exception as e:138 return f"Google Gemini API error: {e}"139 140app = FastAPI()141 142# Serve static files143app.mount("/static", StaticFiles(directory="frontend"), name="static")144 145@app.get("/")146async def home():147 return FileResponse("frontend/index.html")148 149@app.get("/text-generator")150async def text_generator():151 return FileResponse("frontend/text-generator.html")152 153@app.get("/about")154async def about():155 return FileResponse("frontend/about.html")156 157@app.get("/features")158async def features():159 return FileResponse("frontend/features.html")160 161@app.post("/process/")162async def process_file(file: UploadFile = File(...), user_prompt: str = Form(...)):163 file_bytes = await file.read()164 content_type = file.content_type or mimetypes.guess_type(file.filename)[0] or "application/octet-stream"165 166 # Handle image files167 if content_type.startswith("image/"):168 image_url = upload_image(file_bytes)169 170 if not image_url:171 return JSONResponse(172 status_code=500,173 content={"error": "Image upload failed."}174 )175 176 result = generate_response(user_prompt, image_url)177 return {"response": result}178 179 # Handle document files (Excel, PDF, TXT, PowerPoint)180 elif content_type in [181 "application/pdf",182 "application/vnd.ms-excel",183 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",184 "application/vnd.ms-powerpoint",185 "application/vnd.openxmlformats-officedocument.presentationml.presentation",186 "text/plain"187 ]:188 try:189 # Convert the file to text190 text_content = convert_to_text(file_bytes, content_type, file.filename)191 192 # Save text content to a file193 text_filename = f"{Path(file.filename).stem}.txt"194 with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode='w', encoding='utf-8') as text_file:195 text_file.write(text_content)196 text_path = text_file.name197 198 # Upload text file to Cloudinary199 with open(text_path, 'rb') as f:200 text_url = upload_file_to_cloudinary(f.read(), "text")201 202 # Clean up temporary text file203 os.unlink(text_path)204 205 if not text_url:206 return JSONResponse(207 status_code=500,208 content={"error": "Text file upload failed."}209 )210 211 # Process with Gemini212 result = generate_response_for_document(user_prompt, text_url, "text/plain")213 214 return {215 "response": result,216 "text_content": text_content[:500] + "..." if len(text_content) > 500 else text_content,217 "text_url": text_url218 }219 220 except Exception as e:221 return JSONResponse(222 status_code=500,223 content={"error": f"Processing failed: {str(e)}"}224 )225 226 else:227 return JSONResponse(228 status_code=400,229 content={"error": "Unsupported file type. Please upload an image, PDF, Excel, PowerPoint, or text file."}230 )