# Define request body model class InputText(BaseModel): text: str # Load pre-trained sentiment analysis pipeline pipe = pipeline("text-classification", model="avichr/heBERT_sentiment_analysis") # Define sentiment analysis endpoint @app.post("/analyze_sentiment/") async def analyze_sentiment(input_text: InputText): try: # Perform sentiment analysis result = pipe(input_text.text) # Format response response_data = { "text": input_text.text, "sentiment": result[0]["label"], "confidence": result[0]["score"] } # Return response return JSONResponse(content=response_data) except Exception as e: # Return error response if an exception occurs return JSONResponse(status_code=500, content={"error": str(e)}) # Homepage endpoint @app.get("/") async def home(): return RedirectResponse(url="/docs") # Run the FastAPI app if __name__ == "__main__": import nest_asyncio nest_asyncio.apply() ngrok_tunnel = ngrok.connect(8000) print("Public URL:", ngrok_tunnel.public_url) try: import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000) except Exception as e: print(e)