Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,61 @@
|
|
| 1 |
-
# Import necessary libraries
|
| 2 |
import gradio as gr
|
| 3 |
-
from smolagents import
|
| 4 |
|
| 5 |
-
# Step 1:
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
Initialize and return the agent.
|
| 9 |
-
Adjust parameters like model type or configuration as needed.
|
| 10 |
-
"""
|
| 11 |
-
# For example, we initialize an Agent with a sample model
|
| 12 |
-
agent = CodeAgent(tools=[], model=HfApiModel(model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud/')) # Change arguments as per your agent configuration
|
| 13 |
-
return agent
|
| 14 |
-
|
| 15 |
-
# Create the agent instance once so that it persists across user interactions
|
| 16 |
-
agent = create_agent()
|
| 17 |
|
| 18 |
-
|
| 19 |
-
def process_input(user_input):
|
| 20 |
"""
|
| 21 |
-
This function
|
| 22 |
-
|
| 23 |
"""
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
# Step 3:
|
| 29 |
-
|
| 30 |
-
fn=
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
-
# Step 4: Launch the Gradio app
|
| 38 |
if __name__ == "__main__":
|
| 39 |
-
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from smolagents import Agent # adjust the import to your actual smolagents module
|
| 3 |
|
| 4 |
+
# Step 1: Initialize your smolagents agent.
|
| 5 |
+
# Replace "zephyr-7b-beta" with your desired model name/configuration.
|
| 6 |
+
agent = Agent(model="zephyr-7b-beta")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
|
|
|
|
| 9 |
"""
|
| 10 |
+
This function builds the conversation history, calls the smolagents agent,
|
| 11 |
+
and streams the response back to Gradio.
|
| 12 |
"""
|
| 13 |
+
# Build the conversation messages list, starting with the system prompt.
|
| 14 |
+
messages = [{"role": "system", "content": system_message}]
|
| 15 |
+
for user_msg, assistant_msg in history:
|
| 16 |
+
if user_msg:
|
| 17 |
+
messages.append({"role": "user", "content": user_msg})
|
| 18 |
+
if assistant_msg:
|
| 19 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 20 |
+
# Add the latest user input.
|
| 21 |
+
messages.append({"role": "user", "content": message})
|
| 22 |
+
|
| 23 |
+
# Initialize an empty response.
|
| 24 |
+
complete_response = ""
|
| 25 |
+
|
| 26 |
+
# Step 2: Call the agent's chat_completion method.
|
| 27 |
+
# If your smolagents agent supports streaming (i.e. yielding tokens as they are generated),
|
| 28 |
+
# this loop will yield partial responses to update the UI.
|
| 29 |
+
# If streaming is not supported, you can simply do:
|
| 30 |
+
# complete_response = agent.chat_completion(messages, max_tokens, temperature, top_p)
|
| 31 |
+
# yield complete_response
|
| 32 |
+
for token in agent.chat_completion(
|
| 33 |
+
messages,
|
| 34 |
+
max_tokens=max_tokens,
|
| 35 |
+
temperature=temperature,
|
| 36 |
+
top_p=top_p,
|
| 37 |
+
stream=True # set to False if your agent does not support streaming
|
| 38 |
+
):
|
| 39 |
+
complete_response += token
|
| 40 |
+
yield complete_response
|
| 41 |
|
| 42 |
+
# Step 3: Create the Gradio ChatInterface.
|
| 43 |
+
demo = gr.ChatInterface(
|
| 44 |
+
fn=respond,
|
| 45 |
+
additional_inputs=[
|
| 46 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 47 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 48 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 49 |
+
gr.Slider(
|
| 50 |
+
minimum=0.1,
|
| 51 |
+
maximum=1.0,
|
| 52 |
+
value=0.95,
|
| 53 |
+
step=0.05,
|
| 54 |
+
label="Top-p (nucleus sampling)"
|
| 55 |
+
),
|
| 56 |
+
],
|
| 57 |
)
|
| 58 |
|
| 59 |
+
# Step 4: Launch the Gradio app.
|
| 60 |
if __name__ == "__main__":
|
| 61 |
+
demo.launch()
|