Spaces:
Sleeping
Sleeping
Commit
·
5092e07
1
Parent(s):
c5afa64
Try use local model or OpenAI model and Streamlit example
Browse files- Gradio_UI.py +14 -1
- README.md +5 -0
- app.py +47 -12
- requirements.txt +7 -2
- streamlit_app.py +58 -0
Gradio_UI.py
CHANGED
|
@@ -157,7 +157,7 @@ def pull_messages_from_step(
|
|
| 157 |
|
| 158 |
|
| 159 |
def stream_to_gradio(
|
| 160 |
-
agent,
|
| 161 |
task: str,
|
| 162 |
reset_agent_memory: bool = False,
|
| 163 |
additional_args: Optional[dict] = None,
|
|
@@ -175,8 +175,21 @@ def stream_to_gradio(
|
|
| 175 |
for step_log in agent.run(
|
| 176 |
task, stream=True, reset=reset_agent_memory, additional_args=additional_args
|
| 177 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
# Track tokens if model provides them
|
| 179 |
if hasattr(agent.model, "last_input_token_count"):
|
|
|
|
| 180 |
total_input_tokens += agent.model.last_input_token_count
|
| 181 |
total_output_tokens += agent.model.last_output_token_count
|
| 182 |
if isinstance(step_log, ActionStep):
|
|
|
|
| 157 |
|
| 158 |
|
| 159 |
def stream_to_gradio(
|
| 160 |
+
agent: MultiStepAgent,
|
| 161 |
task: str,
|
| 162 |
reset_agent_memory: bool = False,
|
| 163 |
additional_args: Optional[dict] = None,
|
|
|
|
| 175 |
for step_log in agent.run(
|
| 176 |
task, stream=True, reset=reset_agent_memory, additional_args=additional_args
|
| 177 |
):
|
| 178 |
+
if hasattr(step_log, "error") and step_log.error:
|
| 179 |
+
# AttributeError: 'AgentText' object has no attribute 'error'
|
| 180 |
+
# BUG: (this is model endpoint issue)
|
| 181 |
+
# Error in generating model output:
|
| 182 |
+
# (Request ID: UkBnX7)
|
| 183 |
+
# Bad request:
|
| 184 |
+
# Bad Request: Invalid state
|
| 185 |
+
yield gr.ChatMessage(
|
| 186 |
+
role="assistant", content=f"**Error:** {str(step_log.error)}"
|
| 187 |
+
)
|
| 188 |
+
break
|
| 189 |
+
|
| 190 |
# Track tokens if model provides them
|
| 191 |
if hasattr(agent.model, "last_input_token_count"):
|
| 192 |
+
# BUG: TypeError: unsupported operand type(s) for +=: 'int' and 'NoneType'
|
| 193 |
total_input_tokens += agent.model.last_input_token_count
|
| 194 |
total_output_tokens += agent.model.last_output_token_count
|
| 195 |
if isinstance(step_log, ActionStep):
|
README.md
CHANGED
|
@@ -23,5 +23,10 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-
|
|
| 23 |
|
| 24 |
```bash
|
| 25 |
# BUG: somehow failed to inference locally
|
|
|
|
| 26 |
python app.py
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
```
|
|
|
|
| 23 |
|
| 24 |
```bash
|
| 25 |
# BUG: somehow failed to inference locally
|
| 26 |
+
# Gradio
|
| 27 |
python app.py
|
| 28 |
+
# Streamlit
|
| 29 |
+
streamlit run streamlit_app.py
|
| 30 |
+
# Chainlit
|
| 31 |
+
chainlit run chainlit_app.py
|
| 32 |
```
|
app.py
CHANGED
|
@@ -1,6 +1,13 @@
|
|
| 1 |
-
from smolagents import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import datetime
|
| 3 |
-
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools import (
|
|
@@ -9,12 +16,10 @@ from tools import (
|
|
| 9 |
DuckDuckGoSearchTool, # This is also built-in in smolagents
|
| 10 |
)
|
| 11 |
|
| 12 |
-
from Gradio_UI import GradioUI
|
| 13 |
-
|
| 14 |
|
| 15 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 16 |
@tool
|
| 17 |
-
def
|
| 18 |
arg1: str, arg2: int
|
| 19 |
) -> str: # it's import to specify the return type
|
| 20 |
# Keep this format for the description / args / args description but feel free to modify the tool
|
|
@@ -43,12 +48,38 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 43 |
|
| 44 |
|
| 45 |
final_answer = FinalAnswerTool()
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
# Import tool from Hub
|
|
@@ -64,6 +95,8 @@ agent = CodeAgent(
|
|
| 64 |
model=model,
|
| 65 |
tools=[
|
| 66 |
final_answer,
|
|
|
|
|
|
|
| 67 |
image_generation_tool,
|
| 68 |
duckduckgo_tool,
|
| 69 |
visit_webpage_tool,
|
|
@@ -77,5 +110,7 @@ agent = CodeAgent(
|
|
| 77 |
prompt_templates=prompt_templates,
|
| 78 |
)
|
| 79 |
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import (
|
| 2 |
+
CodeAgent,
|
| 3 |
+
HfApiModel,
|
| 4 |
+
TransformersModel,
|
| 5 |
+
OpenAIServerModel,
|
| 6 |
+
load_tool,
|
| 7 |
+
tool,
|
| 8 |
+
)
|
| 9 |
+
import os
|
| 10 |
import datetime
|
|
|
|
| 11 |
import pytz
|
| 12 |
import yaml
|
| 13 |
from tools import (
|
|
|
|
| 16 |
DuckDuckGoSearchTool, # This is also built-in in smolagents
|
| 17 |
)
|
| 18 |
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 21 |
@tool
|
| 22 |
+
def my_custom_tool(
|
| 23 |
arg1: str, arg2: int
|
| 24 |
) -> str: # it's import to specify the return type
|
| 25 |
# Keep this format for the description / args / args description but feel free to modify the tool
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
final_answer = FinalAnswerTool()
|
| 51 |
+
|
| 52 |
+
if IS_IN_HF_SPACE := os.getenv("SPACE_ID"):
|
| 53 |
+
model_id = (
|
| 54 |
+
# BUG: Seems we are failed to call this model => bad request
|
| 55 |
+
"https://wxknx1kg971u7k1n.us-east-1.aws.endpoints.huggingface.cloud" # it is possible that this model may be overloaded
|
| 56 |
+
if True
|
| 57 |
+
# BUG: Model not loaded on the server: https://api-inference.huggingface.co/models/Qwen/Qwen2.5-Coder-32B-Instruct/v1/chat/completions. Please retry with a higher timeout (current: 120)
|
| 58 |
+
# BUG: TooManyRequests: Please log in or use a HF access token
|
| 59 |
+
else "Qwen/Qwen2.5-Coder-32B-Instruct" # The default value of HfApiModel
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
model = HfApiModel(
|
| 63 |
+
max_tokens=2096,
|
| 64 |
+
temperature=0.5,
|
| 65 |
+
model_id=model_id,
|
| 66 |
+
custom_role_conversions=None,
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
else:
|
| 70 |
+
from dotenv import load_dotenv
|
| 71 |
+
|
| 72 |
+
# NOTE: to load the Hugging Face API Key
|
| 73 |
+
curr_dir = os.path.dirname(os.path.abspath(__file__))
|
| 74 |
+
load_dotenv(os.path.join(curr_dir, "../../.env"))
|
| 75 |
+
|
| 76 |
+
if OPENAI_API_KEY := os.getenv("OPENAI_API_KEY"):
|
| 77 |
+
model = OpenAIServerModel(model_id="gpt-3.5-turbo", api_key=OPENAI_API_KEY)
|
| 78 |
+
else:
|
| 79 |
+
# NOTE: this model is not good enough for agent
|
| 80 |
+
model = TransformersModel(
|
| 81 |
+
model_id="HuggingFaceTB/SmolLM2-1.7B-Instruct", trust_remote_code=True
|
| 82 |
+
)
|
| 83 |
|
| 84 |
|
| 85 |
# Import tool from Hub
|
|
|
|
| 95 |
model=model,
|
| 96 |
tools=[
|
| 97 |
final_answer,
|
| 98 |
+
my_custom_tool,
|
| 99 |
+
get_current_time_in_timezone,
|
| 100 |
image_generation_tool,
|
| 101 |
duckduckgo_tool,
|
| 102 |
visit_webpage_tool,
|
|
|
|
| 110 |
prompt_templates=prompt_templates,
|
| 111 |
)
|
| 112 |
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
from Gradio_UI import GradioUI
|
| 115 |
|
| 116 |
+
GradioUI(agent).launch()
|
requirements.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
markdownify
|
| 2 |
-
smolagents
|
| 3 |
requests
|
| 4 |
duckduckgo_search
|
| 5 |
pandas
|
|
@@ -7,4 +7,9 @@ pandas
|
|
| 7 |
# Local development
|
| 8 |
gradio
|
| 9 |
# ImportError: Using SOCKS proxy, but the 'socksio' package is not installed. Make sure to install httpx using `pip install httpx[socks]`
|
| 10 |
-
httpx[socks]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
markdownify
|
| 2 |
+
smolagents[openai]
|
| 3 |
requests
|
| 4 |
duckduckgo_search
|
| 5 |
pandas
|
|
|
|
| 7 |
# Local development
|
| 8 |
gradio
|
| 9 |
# ImportError: Using SOCKS proxy, but the 'socksio' package is not installed. Make sure to install httpx using `pip install httpx[socks]`
|
| 10 |
+
httpx[socks]
|
| 11 |
+
|
| 12 |
+
# Try different UI
|
| 13 |
+
streamlit
|
| 14 |
+
watchdog
|
| 15 |
+
chainlit
|
streamlit_app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
# Import your agent and streaming function.
|
| 4 |
+
# For example, if your agent code is in agent_app.py:
|
| 5 |
+
from app import agent # your CodeAgent instance
|
| 6 |
+
from Gradio_UI import (
|
| 7 |
+
stream_to_gradio,
|
| 8 |
+
) # re-use the generator that yields gr.ChatMessage
|
| 9 |
+
|
| 10 |
+
# (Optionally, if you want to avoid Gradio-specific types you can write your own streaming generator.)
|
| 11 |
+
|
| 12 |
+
st.set_page_config(page_title="CodeAgent Chat", layout="wide")
|
| 13 |
+
st.title("CodeAgent Chat (Streamlit)")
|
| 14 |
+
|
| 15 |
+
# Initialize session state for chat history.
|
| 16 |
+
if "chat_history" not in st.session_state:
|
| 17 |
+
st.session_state.chat_history = []
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def display_chat():
|
| 21 |
+
"""Display the chat history in the app."""
|
| 22 |
+
for message in st.session_state.chat_history:
|
| 23 |
+
role = message.get("role", "assistant")
|
| 24 |
+
content = message.get("content", "")
|
| 25 |
+
if role == "user":
|
| 26 |
+
st.markdown(f"**User:** {content}")
|
| 27 |
+
else:
|
| 28 |
+
st.markdown(f"**Assistant:** {content}")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# Main chat container.
|
| 32 |
+
chat_container = st.container()
|
| 33 |
+
with chat_container:
|
| 34 |
+
display_chat()
|
| 35 |
+
|
| 36 |
+
# TODO: use `st.chat_input`
|
| 37 |
+
|
| 38 |
+
# User input.
|
| 39 |
+
user_input = st.text_input("Enter your message:", key="input_text")
|
| 40 |
+
if st.button("Send") and user_input:
|
| 41 |
+
# Append the user message to the history.
|
| 42 |
+
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
| 43 |
+
with chat_container:
|
| 44 |
+
display_chat()
|
| 45 |
+
|
| 46 |
+
# Stream the agent responses.
|
| 47 |
+
# (Here we are reusing your existing streaming generator.
|
| 48 |
+
# Note that gr.ChatMessage objects have attributes "role" and "content".)
|
| 49 |
+
placeholder = st.empty() # if you want to update a placeholder
|
| 50 |
+
for msg in stream_to_gradio(agent, user_input, reset_agent_memory=False):
|
| 51 |
+
# Extract role and content.
|
| 52 |
+
role = msg.role
|
| 53 |
+
# For content that is not a plain string (e.g. images or audio), you might need to
|
| 54 |
+
# add extra handling. Here we simply convert it to a string.
|
| 55 |
+
content = msg.content if isinstance(msg.content, str) else str(msg.content)
|
| 56 |
+
st.session_state.chat_history.append({"role": role, "content": content})
|
| 57 |
+
with chat_container:
|
| 58 |
+
display_chat()
|