4urears commited on
Commit
abdf449
1 Parent(s): bf38590

Create main.py

Browse files
Files changed (1) hide show
  1. main.py +43 -0
main.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Define request body model
2
+ class InputText(BaseModel):
3
+ text: str
4
+
5
+ # Load pre-trained sentiment analysis pipeline
6
+ pipe = pipeline("text-classification", model="avichr/heBERT_sentiment_analysis")
7
+
8
+ # Define sentiment analysis endpoint
9
+ @app.post("/analyze_sentiment/")
10
+ async def analyze_sentiment(input_text: InputText):
11
+ try:
12
+ # Perform sentiment analysis
13
+ result = pipe(input_text.text)
14
+
15
+ # Format response
16
+ response_data = {
17
+ "text": input_text.text,
18
+ "sentiment": result[0]["label"],
19
+ "confidence": result[0]["score"]
20
+ }
21
+
22
+ # Return response
23
+ return JSONResponse(content=response_data)
24
+ except Exception as e:
25
+ # Return error response if an exception occurs
26
+ return JSONResponse(status_code=500, content={"error": str(e)})
27
+
28
+ # Homepage endpoint
29
+ @app.get("/")
30
+ async def home():
31
+ return RedirectResponse(url="/docs")
32
+
33
+ # Run the FastAPI app
34
+ if __name__ == "__main__":
35
+ import nest_asyncio
36
+ nest_asyncio.apply()
37
+ ngrok_tunnel = ngrok.connect(8000)
38
+ print("Public URL:", ngrok_tunnel.public_url)
39
+ try:
40
+ import uvicorn
41
+ uvicorn.run(app, host="0.0.0.0", port=8000)
42
+ except Exception as e:
43
+ print(e)