Skip to content

Commit

Permalink
Add t5_base Summarization agent abilities
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoober committed Dec 29, 2023
1 parent e29f415 commit 4af74ac
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 8 deletions.
20 changes: 16 additions & 4 deletions integrations/t5-base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,27 @@ The T5 model is a Text-to-Text Transfer Transformer model that was developed by

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

4. **Running The User Script**
4. **Running The User Scripts**

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

For translations, run:
```bash
export T5_BASE_AGENT_ADDRESS="{ agent address from last step }"
poetry run python client.py
poetry run python translation_client.py
```

For summarizations, run:
```bash
export T5_BASE_AGENT_ADDRESS="{ agent address from last step }"
poetry run python summarization_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.
Modify **INPUT_TEXT** in **t5_base_user_translate.py** and **INPUT_TEXT** in **t5_base_user_summarize.py** to translate and summarize different inputs, respectively.

Future modifications/Considerations:
The addition of a new summarize client agent is not strictly necessary, as the two processes could be integrated into a single client agent encompassing both request models and functions. However, they were made separate for testing purposes.

It also seems that both the INPUT_TEXT in the t5_base_user_summarize agent and the payload input_text in the t5_base_agent must be prepended with "summarize:" in order to not be defaulted to a German translation by the model, even though it seems to be summarizing the text as well. I'm not exactly sure why.
34 changes: 31 additions & 3 deletions integrations/t5-base/src/agents/t5_base_agent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from uagents import Agent, Context, Protocol
from messages.t5_base import TranslationRequest, TranslationResponse, Error
from messages.t5_base import TranslationRequest, TranslationResponse, SummarizationRequest, SummarizationResponse, Error
from uagents.setup import fund_agent_if_low
import os
import requests
Expand Down Expand Up @@ -31,7 +31,7 @@
# Ensure the agent has enough funds
fund_agent_if_low(agent.wallet.address())


# translate_text function for base agent
async def translate_text(ctx: Context, sender: str, input_text: str):
# Prepare the data
payload = {
Expand All @@ -50,18 +50,46 @@ async def translate_text(ctx: Context, sender: str, input_text: str):
except Exception as ex:
await ctx.send(sender, Error(error=f"Exception Occurred: {ex}"))
return

# summarize_text function for base agent
async def summarize_text(ctx: Context, sender: str, input_text: str):
# Prepare the data
payload = {
"inputs": f"summarize: {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, SummarizationResponse(summarized_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")


# handle messages of type TranslationRequest
@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)

# handle messages of type SummarizationRequest
@t5_base_agent.on_message(model=SummarizationRequest, replies={SummarizationResponse, Error})
async def handle_request(ctx: Context, sender: str, request: SummarizationRequest):
# Log the request details
ctx.logger.info(f"Got request from {sender}, {request.text}")

await summarize_text(ctx, sender, request.text)


# publish_manifest will make the protocol details available on agentverse.
agent.include(t5_base_agent, publish_manifest=True)
Expand Down
61 changes: 61 additions & 0 deletions integrations/t5-base/src/agents/t5_base_user_summarize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from uagents import Agent, Context, Protocol
from messages.t5_base import SummarizationRequest, SummarizationResponse, Error
from uagents.setup import fund_agent_if_low
import base64
import os

# Replace this input with the text you want to summarize
INPUT_TEXT = """The study examines factors relevant to the projection of CCs (commitment contexts) and assesses their contributions to projection behavior. A model with predicate as a fixed effect shows the highest explanatory power, confirming theoretical claims about the role of embedding, genre, tense, person, and predicate lemma in projection. However, even with all factors considered, the model's Nagelkerke R2 remains at 0.35. Additional analyses incorporating plausibility means of CCs indicate more variability in the data to be accounted for, suggesting the need for further exploration. The CommitmentBank study emphasizes the complexity of understanding projectivity, highlighting the necessity for integrating multiple factors for a comprehensive account."""
# The example text given above is from section 3.5. Summary analyses of the paper https://semanticsarchive.net/Archive/Tg3ZGI2M/Marneffe.pdf used in citation for t5_base on huggingface https://huggingface.co/t5-base#uses. Replace it with your own text for summarization.

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("SummarizationDone", 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=SummarizationRequest)
async def transcript(ctx: Context):
SummarizationDone = ctx.storage.get("SummarizationDone")

if not SummarizationDone:
await ctx.send(T5_BASE_AGENT_ADDRESS, SummarizationRequest(text=f"summarize: {INPUT_TEXT}"))


@t5_base_user.on_message(model=SummarizationResponse)
async def handle_data(ctx: Context, sender: str, response: SummarizationResponse):
ctx.logger.info(f"Summarized text: {response.summarized_text}")
ctx.storage.set("SummarizationDone", 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/messages/t5_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,13 @@ class TranslationResponse(Model):
translated_text: str


class SummarizationRequest(Model):
text: str


class SummarizationResponse(Model):
summarized_text: str


class Error(Model):
error: str
8 changes: 8 additions & 0 deletions integrations/t5-base/src/summarization_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_summarize 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()
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from uagents import Bureau
from agents.t5_base_user import user
from agents.t5_base_user_translate import user

if __name__ == "__main__":
bureau = Bureau(endpoint="http://127.0.0.1:8001/submit", port=8001)
Expand Down

0 comments on commit 4af74ac

Please sign in to comment.