Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,11 +2,16 @@ from transformers import pipeline
|
|
2 |
from fastapi import FastAPI, Request
|
3 |
import uvicorn
|
4 |
|
5 |
-
from uagents import Agent, Context, Bureau
|
6 |
|
7 |
-
#
|
|
|
|
|
|
|
|
|
8 |
emotion_model = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion")
|
9 |
|
|
|
10 |
def analyze_text_metrics(text):
|
11 |
results = emotion_model(text)
|
12 |
text_lower = text.lower()
|
@@ -45,15 +50,15 @@ def analyze_text_metrics(text):
|
|
45 |
|
46 |
return metrics
|
47 |
|
48 |
-
#
|
49 |
agent = Agent(name="sentiment_agent")
|
50 |
|
51 |
-
@agent.on_message()
|
52 |
-
async def handle_message(ctx: Context, sender: str, msg:
|
53 |
-
metrics = analyze_text_metrics(msg)
|
54 |
await ctx.send(sender, str(metrics))
|
55 |
|
56 |
-
# FastAPI
|
57 |
app = FastAPI()
|
58 |
|
59 |
@app.post("/")
|
@@ -63,7 +68,7 @@ async def analyze_text(request: Request):
|
|
63 |
result = analyze_text_metrics(text)
|
64 |
return result
|
65 |
|
66 |
-
#
|
67 |
if __name__ == "__main__":
|
68 |
bureau = Bureau()
|
69 |
bureau.add(agent)
|
|
|
2 |
from fastapi import FastAPI, Request
|
3 |
import uvicorn
|
4 |
|
5 |
+
from uagents import Agent, Context, Bureau, Model
|
6 |
|
7 |
+
# Define input model for uAgents
|
8 |
+
class TextInput(Model):
|
9 |
+
text: str
|
10 |
+
|
11 |
+
# Load Hugging Face emotion classification model
|
12 |
emotion_model = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion")
|
13 |
|
14 |
+
# Custom analysis logic
|
15 |
def analyze_text_metrics(text):
|
16 |
results = emotion_model(text)
|
17 |
text_lower = text.lower()
|
|
|
50 |
|
51 |
return metrics
|
52 |
|
53 |
+
# Set up the uAgent
|
54 |
agent = Agent(name="sentiment_agent")
|
55 |
|
56 |
+
@agent.on_message(model=TextInput)
|
57 |
+
async def handle_message(ctx: Context, sender: str, msg: TextInput):
|
58 |
+
metrics = analyze_text_metrics(msg.text)
|
59 |
await ctx.send(sender, str(metrics))
|
60 |
|
61 |
+
# FastAPI endpoint
|
62 |
app = FastAPI()
|
63 |
|
64 |
@app.post("/")
|
|
|
68 |
result = analyze_text_metrics(text)
|
69 |
return result
|
70 |
|
71 |
+
# Start both agent and FastAPI
|
72 |
if __name__ == "__main__":
|
73 |
bureau = Bureau()
|
74 |
bureau.add(agent)
|