from fastapi import FastAPI, HTTPException from gradio_client import Client import time import asyncio import nest_asyncio import os nest_asyncio.apply() app = FastAPI() # Initialize the client (this loads the API) client = Client(os.getenv("CLIENT_URL")) # System message and initial context system_message = "You are Echo 1.5, developed by Abhinav in collaboration with Iamwomen, a group dedicated to the welfare of women." context = [{"role": "system", "content": system_message}] def chat_with_ai(message): # Add user message to context context.append({"role": "user", "content": message}) # Prepare the full prompt full_prompt = "\n".join([f"{msg['role']}: {msg['content']}" for msg in context]) # Make the API call result = client.predict( message=full_prompt, request=os.getenv('MODEL'), param_3=0.5, param_4=4096, param_5=0.5, param_6=0, api_name="/chat" ) # Add AI response to context context.append({"role": "assistant", "content": result}) return result @app.post("/chat/") async def chat(user_input: str): if not user_input: raise HTTPException(status_code=400, detail="User input is required") response = chat_with_ai(user_input) return {"response": response} @app.get("/") async def root(): return {"message": "Running"} if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=7860)