Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from smolagents import CodeAgent,HfApiModel # adjust the import to your actual smolagents module
|
| 3 |
|
| 4 |
-
# Step 1: Set up your smolagents agent
|
| 5 |
def create_agent():
|
| 6 |
"""
|
| 7 |
Initialize and return the agent.
|
| 8 |
Adjust parameters like model type or configuration as needed.
|
| 9 |
"""
|
| 10 |
-
# For example, we initialize
|
| 11 |
-
agent = CodeAgent(
|
|
|
|
|
|
|
|
|
|
| 12 |
return agent
|
| 13 |
|
| 14 |
-
# Create the agent instance once so that it persists across user interactions
|
| 15 |
agent = create_agent()
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
| 18 |
"""
|
| 19 |
-
|
| 20 |
-
and
|
| 21 |
"""
|
| 22 |
# Build the conversation messages list, starting with the system prompt.
|
| 23 |
messages = [{"role": "system", "content": system_message}]
|
|
@@ -29,24 +43,11 @@ def respond(message, history: list[tuple[str, str]], system_message, max_tokens,
|
|
| 29 |
# Add the latest user input.
|
| 30 |
messages.append({"role": "user", "content": message})
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
# this loop will yield partial responses to update the UI.
|
| 38 |
-
# If streaming is not supported, you can simply do:
|
| 39 |
-
# complete_response = agent.chat_completion(messages, max_tokens, temperature, top_p)
|
| 40 |
-
# yield complete_response
|
| 41 |
-
# for token in agent.chat_completion(
|
| 42 |
-
# messages,
|
| 43 |
-
# max_tokens=max_tokens,
|
| 44 |
-
# temperature=temperature,
|
| 45 |
-
# top_p=top_p,
|
| 46 |
-
# stream=True # set to False if your agent does not support streaming
|
| 47 |
-
# ):
|
| 48 |
-
# complete_response += token
|
| 49 |
-
complete_response=agent.run(messages)
|
| 50 |
yield complete_response
|
| 51 |
|
| 52 |
# Step 3: Create the Gradio ChatInterface.
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from smolagents import CodeAgent, HfApiModel # adjust the import to your actual smolagents module
|
| 3 |
|
| 4 |
+
# Step 1: Set up your smolagents agent.
|
| 5 |
def create_agent():
|
| 6 |
"""
|
| 7 |
Initialize and return the agent.
|
| 8 |
Adjust parameters like model type or configuration as needed.
|
| 9 |
"""
|
| 10 |
+
# For example, we initialize a CodeAgent with a sample model.
|
| 11 |
+
agent = CodeAgent(
|
| 12 |
+
tools=[],
|
| 13 |
+
model=HfApiModel(model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud/')
|
| 14 |
+
)
|
| 15 |
return agent
|
| 16 |
|
| 17 |
+
# Create the agent instance once so that it persists across user interactions.
|
| 18 |
agent = create_agent()
|
| 19 |
|
| 20 |
+
def combine_messages(messages: list[dict]) -> str:
|
| 21 |
+
"""
|
| 22 |
+
Helper function to combine a list of message dictionaries into a single string.
|
| 23 |
+
Each message is prefixed with its role.
|
| 24 |
+
"""
|
| 25 |
+
conversation = ""
|
| 26 |
+
for msg in messages:
|
| 27 |
+
# Capitalize the role (e.g., 'User' instead of 'user') for clarity.
|
| 28 |
+
conversation += f"{msg['role'].capitalize()}: {msg['content']}\n"
|
| 29 |
+
return conversation.strip()
|
| 30 |
+
|
| 31 |
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
| 32 |
"""
|
| 33 |
+
Build the conversation history, combine messages into a single string prompt,
|
| 34 |
+
call the smolagents agent, and stream the response back to Gradio.
|
| 35 |
"""
|
| 36 |
# Build the conversation messages list, starting with the system prompt.
|
| 37 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 43 |
# Add the latest user input.
|
| 44 |
messages.append({"role": "user", "content": message})
|
| 45 |
|
| 46 |
+
# Combine the list of messages into a single string prompt.
|
| 47 |
+
prompt = combine_messages(messages)
|
| 48 |
|
| 49 |
+
# Now call the agent with the prompt.
|
| 50 |
+
complete_response = agent.run(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
yield complete_response
|
| 52 |
|
| 53 |
# Step 3: Create the Gradio ChatInterface.
|