Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,13 @@ from PIL import Image
|
|
| 6 |
#from pydub.playback import Audio
|
| 7 |
from pydub import AudioSegment
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# From transformers import BertModel, BertTokenizer
|
| 11 |
from transformers import HfAgent, load_tool
|
|
@@ -23,16 +30,16 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, Agent, LocalAgent
|
|
| 23 |
# print(agent.run("Is the following `text` (in Spanish) positive or negative?", text="¡Este es un API muy agradable!"))
|
| 24 |
|
| 25 |
# Load tools
|
| 26 |
-
|
| 27 |
-
|
| 28 |
|
| 29 |
|
| 30 |
-
tools = [
|
| 31 |
|
| 32 |
# Define the custom HfAgent class
|
| 33 |
class CustomHfAgent(Agent):
|
| 34 |
def __init__(
|
| 35 |
-
self, url_endpoint, token=os.environ['HF_token'], chat_prompt_template=None, run_prompt_template=None, additional_tools=None
|
| 36 |
):
|
| 37 |
super().__init__(
|
| 38 |
chat_prompt_template=chat_prompt_template,
|
|
@@ -41,12 +48,16 @@ class CustomHfAgent(Agent):
|
|
| 41 |
)
|
| 42 |
self.url_endpoint = url_endpoint
|
| 43 |
self.token = token
|
|
|
|
| 44 |
|
| 45 |
def generate_one(self, prompt, stop):
|
| 46 |
headers = {"Authorization": self.token}
|
|
|
|
|
|
|
| 47 |
inputs = {
|
| 48 |
"inputs": prompt,
|
| 49 |
-
|
|
|
|
| 50 |
}
|
| 51 |
response = requests.post(self.url_endpoint, json=inputs, headers=headers)
|
| 52 |
if response.status_code == 429:
|
|
@@ -63,8 +74,7 @@ class CustomHfAgent(Agent):
|
|
| 63 |
return result[: -len(stop_seq)]
|
| 64 |
return result
|
| 65 |
|
| 66 |
-
|
| 67 |
-
st.title("Hugging Face Agent")
|
| 68 |
|
| 69 |
# Input field for the user's message
|
| 70 |
message = st.text_input("Enter your message:", "")
|
|
@@ -85,46 +95,24 @@ def handle_submission():
|
|
| 85 |
#selected_tools = []
|
| 86 |
selected_tools = [tool for idx, tool in enumerate(tools) if tool_checkboxes[idx]]
|
| 87 |
print(selected_tools)
|
| 88 |
-
#for tool, checkbox in tool_checkboxes:
|
| 89 |
-
# if checkbox:
|
| 90 |
-
# print("checked {tool.name}")
|
| 91 |
-
# selected_tools.append(tool)
|
| 92 |
-
|
| 93 |
-
#selected_tools = [tool for tool, checkbox in tool_checkboxes]
|
| 94 |
|
| 95 |
# Initialize the agent
|
| 96 |
-
agent = CustomHfAgent(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
# Run the agent with the user's message and selected tools
|
| 99 |
response = agent.run(message)
|
| 100 |
#response = agent.chat(message)
|
| 101 |
|
| 102 |
-
print(response)
|
| 103 |
-
|
| 104 |
-
# Display the agent's response
|
| 105 |
-
# if isinstance(response, str):
|
| 106 |
-
# # Display the text response
|
| 107 |
-
# print("text")
|
| 108 |
-
# st.write(response)
|
| 109 |
-
# elif isinstance(response, Image):
|
| 110 |
-
# # Display the image response
|
| 111 |
-
# # print("image")
|
| 112 |
-
# st.image(response)
|
| 113 |
-
# elif isinstance(response, Audio):
|
| 114 |
-
# print("audio")
|
| 115 |
-
# # Handle audio response (replace with your audio rendering code)
|
| 116 |
-
# st.audio(response)
|
| 117 |
-
# else:
|
| 118 |
-
# # Handle unrecognized response type
|
| 119 |
-
# print("warning")
|
| 120 |
-
# st.warning("Unrecognized response type.")
|
| 121 |
-
# Update the import statement for Audio
|
| 122 |
-
|
| 123 |
-
# ...
|
| 124 |
|
| 125 |
# Display the agent's response
|
| 126 |
if response is None:
|
| 127 |
-
st.warning("The agent's response is None.")
|
| 128 |
elif isinstance(response, Image.Image):
|
| 129 |
# Display the image response
|
| 130 |
st.image(response)
|
|
@@ -144,27 +132,12 @@ def handle_submission():
|
|
| 144 |
st.write(response)
|
| 145 |
else:
|
| 146 |
# Handle unrecognized response type
|
| 147 |
-
st.warning("Unrecognized response type.")
|
| 148 |
|
| 149 |
|
| 150 |
-
# Display the agent's response
|
| 151 |
-
# Display the agent's response
|
| 152 |
-
#if response.startswith("Image:"):
|
| 153 |
-
# # Display the image response
|
| 154 |
-
# image_data = base64.b64decode(response.split(",")[1])
|
| 155 |
-
# img = Image.open(io.BytesIO(image_data))
|
| 156 |
-
# st.image(img)
|
| 157 |
-
#else:
|
| 158 |
-
# # Display the text response
|
| 159 |
-
# st.write(response)
|
| 160 |
-
|
| 161 |
-
# Add a button to trigger the agent to respond again
|
| 162 |
-
#st.button("Ask Again
|
| 163 |
-
#st.button("Ask Again", key="ask_again_btn")
|
| 164 |
|
| 165 |
# Add the callback function to the Streamlit app
|
| 166 |
submit_button = st.button("Submit", on_click=handle_submission)
|
| 167 |
-
#st.button("Ask Again")(handle_submission)
|
| 168 |
|
| 169 |
# Define a callback function to handle the button click
|
| 170 |
def ask_again():
|
|
@@ -175,4 +148,4 @@ def ask_again():
|
|
| 175 |
agent.run("")
|
| 176 |
|
| 177 |
# Add the callback function to the button
|
| 178 |
-
|
|
|
|
| 6 |
#from pydub.playback import Audio
|
| 7 |
from pydub import AudioSegment
|
| 8 |
|
| 9 |
+
import IPython
|
| 10 |
+
import soundfile as sf
|
| 11 |
+
|
| 12 |
+
def play_audio(audio):
|
| 13 |
+
sf.write("speech_converted.wav", audio.numpy(), samplerate=16000)
|
| 14 |
+
return IPython.display.Audio("speech_converted.wav")
|
| 15 |
+
|
| 16 |
|
| 17 |
# From transformers import BertModel, BertTokenizer
|
| 18 |
from transformers import HfAgent, load_tool
|
|
|
|
| 30 |
# print(agent.run("Is the following `text` (in Spanish) positive or negative?", text="¡Este es un API muy agradable!"))
|
| 31 |
|
| 32 |
# Load tools
|
| 33 |
+
random-character-tool = load_tool("Chris4K/random-character-tool")
|
| 34 |
+
text-generation-tool = load_tool("Chris4K/text-generation-tool")
|
| 35 |
|
| 36 |
|
| 37 |
+
tools = [random-character-tool, text-generation-tool]
|
| 38 |
|
| 39 |
# Define the custom HfAgent class
|
| 40 |
class CustomHfAgent(Agent):
|
| 41 |
def __init__(
|
| 42 |
+
self, url_endpoint, token=os.environ['HF_token'], chat_prompt_template=None, run_prompt_template=None, additional_tools=None, input_params=None
|
| 43 |
):
|
| 44 |
super().__init__(
|
| 45 |
chat_prompt_template=chat_prompt_template,
|
|
|
|
| 48 |
)
|
| 49 |
self.url_endpoint = url_endpoint
|
| 50 |
self.token = token
|
| 51 |
+
self.input_params = input_params
|
| 52 |
|
| 53 |
def generate_one(self, prompt, stop):
|
| 54 |
headers = {"Authorization": self.token}
|
| 55 |
+
# Use the value from input_params or a default value if not provided
|
| 56 |
+
max_new_tokens = self.input_params.get("max_new_tokens", 192)
|
| 57 |
inputs = {
|
| 58 |
"inputs": prompt,
|
| 59 |
+
# Here the max_new_token varies from default 200 which leads to an error
|
| 60 |
+
"parameters": {"max_new_tokens": max_new_tokens, "return_full_text": False, "stop": stop},
|
| 61 |
}
|
| 62 |
response = requests.post(self.url_endpoint, json=inputs, headers=headers)
|
| 63 |
if response.status_code == 429:
|
|
|
|
| 74 |
return result[: -len(stop_seq)]
|
| 75 |
return result
|
| 76 |
|
| 77 |
+
st.title("Hugging Face Agent and tools")
|
|
|
|
| 78 |
|
| 79 |
# Input field for the user's message
|
| 80 |
message = st.text_input("Enter your message:", "")
|
|
|
|
| 95 |
#selected_tools = []
|
| 96 |
selected_tools = [tool for idx, tool in enumerate(tools) if tool_checkboxes[idx]]
|
| 97 |
print(selected_tools)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
# Initialize the agent
|
| 100 |
+
agent = CustomHfAgent(
|
| 101 |
+
url_endpoint="https://api-inference.huggingface.co/models/bigcode/starcoder",
|
| 102 |
+
token=os.environ['HF_token'],
|
| 103 |
+
additional_tools=selected_tools,
|
| 104 |
+
input_params={"max_new_tokens": 192}, # Set the desired value
|
| 105 |
+
)
|
| 106 |
|
| 107 |
# Run the agent with the user's message and selected tools
|
| 108 |
response = agent.run(message)
|
| 109 |
#response = agent.chat(message)
|
| 110 |
|
| 111 |
+
print("Response " + response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
# Display the agent's response
|
| 114 |
if response is None:
|
| 115 |
+
st.warning("The agent's response is None. Please try again. For Example: Generate an image of a boat in the water")
|
| 116 |
elif isinstance(response, Image.Image):
|
| 117 |
# Display the image response
|
| 118 |
st.image(response)
|
|
|
|
| 132 |
st.write(response)
|
| 133 |
else:
|
| 134 |
# Handle unrecognized response type
|
| 135 |
+
st.warning("Unrecognized response type. Please try again. For Example: Generate an image of a boat in the water")
|
| 136 |
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
# Add the callback function to the Streamlit app
|
| 140 |
submit_button = st.button("Submit", on_click=handle_submission)
|
|
|
|
| 141 |
|
| 142 |
# Define a callback function to handle the button click
|
| 143 |
def ask_again():
|
|
|
|
| 148 |
agent.run("")
|
| 149 |
|
| 150 |
# Add the callback function to the button
|
| 151 |
+
ask_again = st.button("Ask again", on_click=ask_again)
|