Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,50 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
x = st.slider('Select a value')
|
4 |
-
st.write(x, 'squared is', x * x)
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import BertModel, BertTokenizer
|
3 |
+
from transformers import HfAgent, load_tool
|
4 |
+
|
5 |
+
# Load tools
|
6 |
+
controlnet_transformer = load_tool("huggingface-tools/text-to-image")
|
7 |
+
upscaler = load_tool("diffusers/latent-upscaler-tool")
|
8 |
+
|
9 |
+
tools = [controlnet_transformer, upscaler ]
|
10 |
+
|
11 |
+
# Define the model and tokenizer
|
12 |
+
model = BertModel.from_pretrained('bert-base-uncased')
|
13 |
+
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
|
14 |
+
|
15 |
+
# Create the Streamlit app
|
16 |
+
st.title("Hugging Face Agent")
|
17 |
+
|
18 |
+
# Input field for the user's message
|
19 |
+
message_input = st.text_input("Enter your message:", "")
|
20 |
+
|
21 |
+
# Checkboxes for the tools to be used by the agent
|
22 |
+
tool_checkboxes = [st.checkbox(f"Use {tool}") for tool in tools]
|
23 |
+
|
24 |
+
# Submit button
|
25 |
+
submit_button = st.button("Submit")
|
26 |
+
|
27 |
+
# Define the callback function to handle the form submission
|
28 |
+
def handle_submission():
|
29 |
+
# Get the user's message and the selected tools
|
30 |
+
message = message_input
|
31 |
+
selected_tools = [tool for tool, checkbox in zip(tools, tool_checkboxes) if checkbox]
|
32 |
+
|
33 |
+
# Initialize the agent with the selected tools
|
34 |
+
agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder", additional_tools=tools)
|
35 |
+
|
36 |
+
agent.config.tokenizer = tokenizer
|
37 |
+
agent.config.tools = selected_tools
|
38 |
+
|
39 |
+
# Process the user's message
|
40 |
+
inputs = tokenizer.encode_plus(message, add_special_tokens=True, return_tensors="pt")
|
41 |
+
outputs = agent(inputs['input_ids'], attention_mask=inputs['attention_mask'])
|
42 |
+
|
43 |
+
# Display the agent's response
|
44 |
+
response = outputs.logits[0].item()
|
45 |
+
st.text(f"{response:.4f}")
|
46 |
+
|
47 |
+
# Add the callback function to the Streamlit app
|
48 |
+
submit_button = st.button("Submit", on_click=handle_submission)
|
49 |
+
|
50 |
|
|
|
|