forked from fetchai/uAgents
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
T5 Base Model Integration (fetchai#162)
- Loading branch information
1 parent
268af59
commit 2b4b142
Showing
8 changed files
with
1,454 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |