from fastapi import FastAPI, HTTPException, Request, BackgroundTasks, Form from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from httpx import Timeout import httpx import logging import firebase_admin from firebase_admin import credentials, firestore from google.oauth2 import service_account from googleapiclient.discovery import build from googleapiclient.http import MediaIoBaseDownload from io import BytesIO import google.generativeai as genai import os import requests # Initialize FastAPI app = FastAPI() # Allow all origins app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Set up logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logging.info("Application starting up") # Timeout configuration request_timeout = Timeout(1000.0, connect=600.0) # Gemini model configuration # Load Firebase configuration from a file with open('/app/secrets/gemini_key.txt', 'r') as f: api_key = f.read() genai.configure(api_key=api_key) # Create the model generation_config = { "temperature": 1, "top_p": 0.95, "top_k": 64, "max_output_tokens": 8192, "response_mime_type": "application/json", } model = genai.GenerativeModel( model_name="gemini-1.5-flash", generation_config=generation_config, # safety_settings = Adjust safety settings # See https://ai.google.dev/gemini-api/docs/safety-settings ) @app.post("/upload-pdf") async def upload_drive_document(request: Request): # Parse JSON payload data = await request.json() file_id = data.get("file_id") """ Endpoint to upload a document from Google Drive to Gemini """ try: # Download file from Google Drive file_content, mime_type = download_file_from_drive(file_id) temp_file_path = "/tmp/downloaded_file.pdf" with open(temp_file_path, "wb") as f: f.write(file_content.getbuffer()) # Log the temporary file path logging.info(f"File saved to {temp_file_path}") # 2024-08-09 07:06:19,925 - INFO - Response is genai.File({ # 'name': 'files/gf1gtp9zvk54', # 'display_name': 'testing', # 'mime_type': 'application/pdf', # 'sha256_hash': 'NDllZjNlMTE2ODQ1YjMxMGIxYmRiMTA3N2FhYzc3YWRiYjAxMjExZmEwZThkMmNiMzJkMjAwMjY2NDg1OGY1ZA==', # 'size_bytes': '1725728', # 'state': 'ACTIVE', # 'uri': 'https://generativelanguage.googleapis.com/v1beta/files/gf1gtp9zvk54', # 'create_time': '2024-08-09T07:06:19.639959Z', # 'expiration_time': '2024-08-11T07:06:19.590668644Z', # 'update_time': '2024-08-09T07:06:19.639959Z'}) # Use the file path if needed # For example, upload to Gemini using the file path: response = genai.upload_file(path=temp_file_path, display_name="testing") # Clean up: remove the temporary file after processing os.remove(temp_file_path) logging.info(f"Response is {response}") # title must be lower case and no - # Generate content using the model ai_response = model.generate_content( ["Provide answer sheet with the following property: {\"answer_sheet\": [{\"question_number\": \"1\", \"correct_answer\": \"B\"}]", response] ) genai.delete_file(response.name) return {"status": "success", "gemini_response": ai_response.text} except Exception as e: logging.error(f"Error uploading document: {e}") raise HTTPException(status_code=500, detail="Failed to upload document") def download_file_from_drive(file_id: str) -> (BytesIO, str): """ Downloads a file from Google Drive using the file ID and returns a BytesIO stream and the MIME type. """ try: # URL for downloading the file from Google Drive url = f"https://drive.google.com/uc?export=download&id={file_id}" # Send a GET request to the URL response = requests.get(url, stream=True) # Check if the request was successful if response.status_code == 200: # Create a BytesIO object from the response content file_io = BytesIO(response.content) # Get the MIME type of the file from the response headers mime_type = response.headers.get('Content-Type', 'application/octet-stream') return file_io, mime_type else: raise HTTPException(status_code=500, detail=f"Failed to download file, status code: {response.status_code}") except Exception as e: logging.error(f"Error downloading file from Drive: {e}") raise HTTPException(status_code=500, detail="Failed to download file from Drive") @app.post("/test") async def test(): """ Endpoint to send a URL to the Gemini model and receive a summary """ try: # Create a GenerativeModel instance model = genai.GenerativeModel(model_name="models/gemini-1.5-flash") # title must be lower case and no - file = genai.get_file(name="files/gf1gtp9zvk54") # Generate content using the model response = model.generate_content( ["Provide answers to the exam questions.", file] ) logging.info(response) # Return the generated text return {"status": "success", "generated_text": response.text} except Exception as e: logging.error(f"Error generating content: {e}") raise HTTPException(status_code=500, detail="Failed to generate content")