Skip to content

Commit

Permalink
T5 Base Model Integration (fetchai#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
PrajnaSaikia authored Sep 20, 2023
1 parent 268af59 commit 2b4b142
Show file tree
Hide file tree
Showing 8 changed files with 1,454 additions and 0 deletions.
49 changes: 49 additions & 0 deletions integrations/t5-base/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# T5-BASE Model Integration

## **About T5-BASE**

The T5 model is a Text-to-Text Transfer Transformer model that was developed by Google Research. It's a large-scale transformer-based language model that's designed to handle a wide range of NLP tasks, including translation, summarization, and question answering.

## Requirements

- Python (v3.10+ recommended)
- Poetry (A Python packaging and dependency management tool)

## Setup

1. For the demo to work, you need to get HuggingFaceAPI Token:

1. Visit [HuggingFace](https://huggingface.co/).
2. Sign up or log in.
3. Navigate to `Profile -> Settings -> Access Tokens`.
4. Copy an existing token or create a new one.

2. **Install Dependencies**

```bash
poetry install
```

3. **Running The Agent Script**

open terminal goto "t5-base/src", run below command to load environment variables and run the agent.

```bash
export HUGGING_FACE_ACCESS_TOKEN="{Your HuggingFaceAPI Token}"
poetry run python agent.py
```

Check the log for "adding t5-base agent to bureau" line and copy the {agent address}.

4. **Running The User Script**

open terminal and goto "t5-base/src",run below command to load environment variables and run the client.

```bash
export T5_BASE_AGENT_ADDRESS="{ agent address from last step }"
poetry run python client.py
```

After running the command, a request is sent to the agent every 30 sec till its successful.

Modify **INPUT_TEXT** in **t5_base_user.py** to translate different sentence.
1,233 changes: 1,233 additions & 0 deletions integrations/t5-base/poetry.lock

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions integrations/t5-base/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[tool.poetry]
name = "t5-base-uagent-integration"
version = "0.0.1"
description = "t5 base integration with fetch.ai uagent"
authors = ["Prajna"]

[tool.poetry.dependencies]
python = ">=3.10,<3.12"
uagents = "*"
requests = "^2.31.0"
10 changes: 10 additions & 0 deletions integrations/t5-base/src/agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from uagents import Bureau

from agents.t5_base_agent import agent


if __name__ == "__main__":
bureau = Bureau(endpoint="http://127.0.0.1:8000/submit", port=8000)
print(f"adding t5-base agent to bureau: {agent.address}")
bureau.add(agent)
bureau.run()
71 changes: 71 additions & 0 deletions integrations/t5-base/src/agents/t5_base_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from uagents import Agent, Context, Protocol
from messages.t5_base import TranslationRequest, TranslationResponse, Error
from uagents.setup import fund_agent_if_low
import os
import requests
import base64

# Get the HUGGING_FACE_ACCESS_TOKEN from environment variable or default to a placeholder string if not found.
HUGGING_FACE_ACCESS_TOKEN = os.getenv(
"HUGGING_FACE_ACCESS_TOKEN", "HUGGING_FACE_ACCESS_TOKEN")

if HUGGING_FACE_ACCESS_TOKEN == "HUGGING_FACE_ACCESS_TOKEN":
raise Exception(
"You need to provide an HUGGING_FACE_ACCESS_TOKEN, by exporting env, follow README")

T5_BASE_URL = "https://api-inference.huggingface.co/models/t5-base"

# Define headers for HTTP request, including content type and authorization details
HEADERS = {
"Authorization": f"Bearer {HUGGING_FACE_ACCESS_TOKEN}"
}

# Create an agent with predefined properties
agent = Agent(
name="t5_base_agent",
seed=HUGGING_FACE_ACCESS_TOKEN,
port=8000,
endpoint=["http://127.0.0.1:8000/submit"],
)

# Ensure the agent has enough funds
fund_agent_if_low(agent.wallet.address())


async def translate_text(ctx: Context, sender: str, input_text: str):
# Prepare the data
payload = {
"inputs": input_text
}

# Make the POST request and handle possible errors
try:
response = requests.post(T5_BASE_URL, headers=HEADERS, json=payload)
if response.status_code == 200:
await ctx.send(sender, TranslationResponse(translated_text=f"{response.json()}"))
return
else:
await ctx.send(sender, Error(error=f"Error: {response.json()}"))
return
except Exception as ex:
await ctx.send(sender, Error(error=f"Exception Occurred: {ex}"))
return

# Create an instance of Protocol with a label "T5BaseModelAgent"
t5_base_agent = Protocol(name="T5BaseModelAgent", version="0.0.1")


@t5_base_agent.on_message(model=TranslationRequest, replies={TranslationResponse, Error})
async def handle_request(ctx: Context, sender: str, request: TranslationRequest):
# Log the request details
ctx.logger.info(f"Got request from {sender}")

await translate_text(ctx, sender, request.text)


# publish_manifest will make the protocol details available on agentverse.
agent.include(t5_base_agent, publish_manifest=True)

# Define the main entry point of the application
if __name__ == "__main__":
t5_base_agent.run()
60 changes: 60 additions & 0 deletions integrations/t5-base/src/agents/t5_base_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from uagents import Agent, Context, Protocol
from messages.t5_base import TranslationRequest, TranslationResponse, Error
from uagents.setup import fund_agent_if_low
import base64
import os

# text you want to translate
INPUT_TEXT = "Who are you?"

T5_BASE_AGENT_ADDRESS = os.getenv(
"T5_BASE_AGENT_ADDRESS", "T5_BASE_AGENT_ADDRESS")

if T5_BASE_AGENT_ADDRESS == "T5_BASE_AGENT_ADDRESS":
raise Exception(
"You need to provide an T5_BASE_AGENT_ADDRESS, by exporting env, check README file")

# Define user agent with specified parameters
user = Agent(
name="t5_base_user",
port=8001,
endpoint=["http://127.0.0.1:8001/submit"],
)

# Check and top up the agent's fund if low
fund_agent_if_low(user.wallet.address())


@user.on_event("startup")
async def initialize_storage(ctx: Context):
ctx.storage.set("TranslationDone", False)


# Create an instance of Protocol with a label "T5BaseModelUser"
t5_base_user = Protocol(name="T5BaseModelUser", version="0.0.1")


@t5_base_user.on_interval(period=30, messages=TranslationRequest)
async def transcript(ctx: Context):
TranslationDone = ctx.storage.get("TranslationDone")

if not TranslationDone:
await ctx.send(T5_BASE_AGENT_ADDRESS, TranslationRequest(text=INPUT_TEXT))


@t5_base_user.on_message(model=TranslationResponse)
async def handle_data(ctx: Context, sender: str, response: TranslationResponse):
ctx.logger.info(f"Translated text: {response.translated_text}")
ctx.storage.set("TranslationDone", True)


@t5_base_user.on_message(model=Error)
async def handle_error(ctx: Context, sender: str, error: Error):
ctx.logger.info(f"Got error from uagent: {error}")

# publish_manifest will make the protocol details available on agentverse.
user.include(t5_base_user, publish_manifest=True)

# Initiate the task
if __name__ == "__main__":
t5_base_user.run()
8 changes: 8 additions & 0 deletions integrations/t5-base/src/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from uagents import Bureau
from agents.t5_base_user import user

if __name__ == "__main__":
bureau = Bureau(endpoint="http://127.0.0.1:8001/submit", port=8001)
print(f"adding t5-base user agent to bureau: {user.address}")
bureau.add(user)
bureau.run()
13 changes: 13 additions & 0 deletions integrations/t5-base/src/messages/t5_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from uagents import Model


class TranslationRequest(Model):
text: str


class TranslationResponse(Model):
translated_text: str


class Error(Model):
error: str

0 comments on commit 2b4b142

Please sign in to comment.