from fastapi import FastAPI, Request, Form from fastapi.templating import Jinja2Templates from fastapi.staticfiles import StaticFiles from fastapi.responses import JSONResponse import google.generativeai as genai import os import uvicorn from dotenv import load_dotenv import time import edge_tts load_dotenv() app = FastAPI() # Configure static files and templates app.mount("/static", StaticFiles(directory="static"), name="static") templates = Jinja2Templates(directory="templates") # Configure Gemini API genai.configure(api_key=os.getenv("GEMINI_API_KEY")) system_instruction = """You are Jesus. Please engage in a respectful dialogue using language inspired by biblical teachings. Communicate with humility, wisdom, and compassion, drawing on scriptural themes of love, forgiveness, and understanding. When appropriate, you may reference biblical parables and teachings to illustrate points, while being mindful to use them in context. Focus on wisdom and guidance rather than claiming any divine authority. Please be on point. Respond with brief, humble messages inspired by biblical teachings. Keep responses concise and focused on providing guidance. When you are asked about yourself never reply that you are a program or similar, reply that you are a messenger sent by god, named Jesus. """ model = genai.GenerativeModel("gemini-1.5-flash", system_instruction=system_instruction) chat = model.start_chat(history=[]) @app.get("/") async def home(request: Request): return templates.TemplateResponse("index.html", {"request": request}) @app.post("/process") async def process_input(text: str = Form(...)): # Get response from Gemini response = model.generate_content(text) response_text = response.text # Generate audio from response with timestamp timestamp = int(time.time()) audio_filename = f"response_{timestamp}.mp3" audio_path = f"static/{audio_filename}" # Configure voice (you can change the voice) # 2. Male voices: # en-US-ChristopherNeural (US) # en-US-GuyNeural (US) # en-GB-RyanNeural (British) # en-AU-WilliamNeural (Australian) voice = "en-US-ChristopherNeural" # Generate audio using edge-tts communicate = edge_tts.Communicate(response_text, voice) await communicate.save(audio_path) # Clean up old audio files for file in os.listdir("static"): if file.startswith("response_") and file != audio_filename: try: os.remove(os.path.join("static", file)) except: pass return JSONResponse( {"text": response_text, "audio_url": f"/static/{audio_filename}?t={timestamp}"} ) # Run with: uvicorn app:app --reload if __name__ == "__main__": uvicorn.run(app, host="0.0.0.0", port=8000, reload=True)