Update app.py
Browse files
app.py
CHANGED
|
@@ -6,14 +6,14 @@ import torch
|
|
| 6 |
from transformers import AutoModelForCausalLM, AutoTokenizer, LocalAgent
|
| 7 |
|
| 8 |
|
| 9 |
-
checkpoint = "THUDM/agentlm-7b"
|
| 10 |
-
model = AutoModelForCausalLM.from_pretrained(checkpoint, device_map="auto", torch_dtype=torch.bfloat16)
|
| 11 |
-
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
| 12 |
|
| 13 |
-
agent = LocalAgent(model, tokenizer)
|
| 14 |
-
agent.run("Draw me a picture of rivers and lakes.")
|
| 15 |
|
| 16 |
-
print(agent.run("Is the following `text` (in Spanish) positive or negative?", text="¡Este es un API muy agradable!"))
|
| 17 |
|
| 18 |
# Load tools
|
| 19 |
controlnet_transformer = load_tool("huggingface-tools/text-to-image")
|
|
@@ -21,6 +21,127 @@ upscaler = load_tool("diffusers/latent-upscaler-tool")
|
|
| 21 |
|
| 22 |
tools = [controlnet_transformer, upscaler ]
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# Define the model and tokenizer
|
| 25 |
#model = BertModel.from_pretrained('bert-base-uncased')
|
| 26 |
#tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
|
|
|
| 6 |
from transformers import AutoModelForCausalLM, AutoTokenizer, LocalAgent
|
| 7 |
|
| 8 |
|
| 9 |
+
#checkpoint = "THUDM/agentlm-7b"
|
| 10 |
+
#model = AutoModelForCausalLM.from_pretrained(checkpoint, device_map="auto", torch_dtype=torch.bfloat16)
|
| 11 |
+
#tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
| 12 |
|
| 13 |
+
#agent = LocalAgent(model, tokenizer)
|
| 14 |
+
#agent.run("Draw me a picture of rivers and lakes.")
|
| 15 |
|
| 16 |
+
#print(agent.run("Is the following `text` (in Spanish) positive or negative?", text="¡Este es un API muy agradable!"))
|
| 17 |
|
| 18 |
# Load tools
|
| 19 |
controlnet_transformer = load_tool("huggingface-tools/text-to-image")
|
|
|
|
| 21 |
|
| 22 |
tools = [controlnet_transformer, upscaler ]
|
| 23 |
|
| 24 |
+
|
| 25 |
+
############ HfAgent
|
| 26 |
+
from huggingface_hub import login
|
| 27 |
+
#Do this before HfAgent() and it should work
|
| 28 |
+
|
| 29 |
+
#from huggingface_hub import login
|
| 30 |
+
# load tools
|
| 31 |
+
from transformers.tools import HfAgent
|
| 32 |
+
from transformers.tools import Agent
|
| 33 |
+
#import textract
|
| 34 |
+
#from utils import logging
|
| 35 |
+
import time
|
| 36 |
+
|
| 37 |
+
from huggingface_hub import HfFolder, hf_hub_download, list_spaces
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class CustomHfAgent(Agent):
|
| 43 |
+
"""
|
| 44 |
+
Agent that uses an inference endpoint to generate code.
|
| 45 |
+
|
| 46 |
+
Args:
|
| 47 |
+
url_endpoint (`str`):
|
| 48 |
+
The name of the url endpoint to use.
|
| 49 |
+
token (`str`, *optional*):
|
| 50 |
+
The token to use as HTTP bearer authorization for remote files. If unset, will use the token generated when
|
| 51 |
+
running `huggingface-cli login` (stored in `~/.huggingface`).
|
| 52 |
+
chat_prompt_template (`str`, *optional*):
|
| 53 |
+
Pass along your own prompt if you want to override the default template for the `chat` method. Can be the
|
| 54 |
+
actual prompt template or a repo ID (on the Hugging Face Hub). The prompt should be in a file named
|
| 55 |
+
`chat_prompt_template.txt` in this repo in this case.
|
| 56 |
+
run_prompt_template (`str`, *optional*):
|
| 57 |
+
Pass along your own prompt if you want to override the default template for the `run` method. Can be the
|
| 58 |
+
actual prompt template or a repo ID (on the Hugging Face Hub). The prompt should be in a file named
|
| 59 |
+
`run_prompt_template.txt` in this repo in this case.
|
| 60 |
+
additional_tools ([`Tool`], list of tools or dictionary with tool values, *optional*):
|
| 61 |
+
Any additional tools to include on top of the default ones. If you pass along a tool with the same name as
|
| 62 |
+
one of the default tools, that default tool will be overridden.
|
| 63 |
+
|
| 64 |
+
Example:
|
| 65 |
+
|
| 66 |
+
```py
|
| 67 |
+
from transformers import HfAgent
|
| 68 |
+
|
| 69 |
+
agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder")
|
| 70 |
+
agent.run("Is the following `text` (in Spanish) positive or negative?", text="¡Este es un API muy agradable!")
|
| 71 |
+
```
|
| 72 |
+
"""
|
| 73 |
+
|
| 74 |
+
def __init__(
|
| 75 |
+
self, url_endpoint, token=None, chat_prompt_template=None, run_prompt_template=None, additional_tools=None
|
| 76 |
+
):
|
| 77 |
+
# super()._init_(self, url_endpoint, token=None, chat_prompt_template=None, run_prompt_template=None, additional_tools=None)
|
| 78 |
+
self.url_endpoint = url_endpoint
|
| 79 |
+
if token is None:
|
| 80 |
+
self.token = f"Bearer {HfFolder().get_token()}"
|
| 81 |
+
elif token.startswith("Bearer") or token.startswith("Basic"):
|
| 82 |
+
self.token = token
|
| 83 |
+
else:
|
| 84 |
+
self.token = f"Bearer {token}"
|
| 85 |
+
super().__init__(
|
| 86 |
+
chat_prompt_template=chat_prompt_template,
|
| 87 |
+
run_prompt_template=run_prompt_template,
|
| 88 |
+
additional_tools=additional_tools,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
def generate_one(self, prompt, stop):
|
| 92 |
+
headers = {"Authorization": self.token}
|
| 93 |
+
inputs = {
|
| 94 |
+
"inputs": prompt,
|
| 95 |
+
"parameters": {"max_new_tokens": 192, "return_full_text": False, "stop": stop},
|
| 96 |
+
}
|
| 97 |
+
print(inputs)
|
| 98 |
+
response = requests.post(self.url_endpoint, json=inputs, headers=headers)
|
| 99 |
+
if response.status_code == 429:
|
| 100 |
+
print("Getting rate-limited, waiting a tiny bit before trying again.")
|
| 101 |
+
time.sleep(1)
|
| 102 |
+
return self._generate_one(prompt)
|
| 103 |
+
elif response.status_code != 200:
|
| 104 |
+
raise ValueError(f"Errors {inputs} {response.status_code}: {response.json()}")
|
| 105 |
+
|
| 106 |
+
result = response.json()[0]["generated_text"]
|
| 107 |
+
# Inference API returns the stop sequence
|
| 108 |
+
for stop_seq in stop:
|
| 109 |
+
if result.endswith(stop_seq):
|
| 110 |
+
return result[: -len(stop_seq)]
|
| 111 |
+
return result
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
# create agent
|
| 117 |
+
#agent = HfAgent(API_URL)
|
| 118 |
+
|
| 119 |
+
#print(agent)
|
| 120 |
+
# instruct agent
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
# Use CustomHfAgent in your code
|
| 124 |
+
agent = CustomHfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder")
|
| 125 |
+
#agent.token = "Bearer xxx"
|
| 126 |
+
#print(agent.token)
|
| 127 |
+
#agent.run("Answer the following question", question ="what is the capitol of the usa?", context="The capitol of the usa is London")
|
| 128 |
+
#agent.chat("Draw me a picture of rivers and lakes")
|
| 129 |
+
|
| 130 |
+
#agent.chat("Transform the picture so that there is a rock in there")
|
| 131 |
+
|
| 132 |
+
#result = agent.generate_one("What is the capitol of the usa.", stop=["your_stop_sequence"])
|
| 133 |
+
#print(result)
|
| 134 |
+
|
| 135 |
+
#agent.run("Show me an image of a horse")
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
#####
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
|
| 145 |
# Define the model and tokenizer
|
| 146 |
#model = BertModel.from_pretrained('bert-base-uncased')
|
| 147 |
#tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|