Update app.py
Browse files
app.py
CHANGED
|
@@ -1,88 +1,88 @@
|
|
| 1 |
|
| 2 |
-
# Import
|
| 3 |
-
# CodeAgent helps us
|
| 4 |
-
# HfApiModel
|
| 5 |
-
# The @tool decorator marks functions as "tools"
|
| 6 |
from smolagents import CodeAgent, HfApiModel, tool
|
| 7 |
|
| 8 |
# ------------------------------------------------------------------------------
|
| 9 |
-
# Import YAML so we can load additional instructions from a
|
| 10 |
import yaml
|
| 11 |
|
| 12 |
# ------------------------------------------------------------------------------
|
| 13 |
-
# Import our helper tool for preparing the final
|
| 14 |
-
# FinalAnswerTool packages the final answer.
|
| 15 |
# GradioUI creates a simple webpage for user interaction.
|
| 16 |
from tools.final_answer import FinalAnswerTool
|
| 17 |
from Gradio_UI import GradioUI
|
| 18 |
|
| 19 |
# ------------------------------------------------------------------------------
|
| 20 |
-
# Set up our AI model (the "brain" of
|
| 21 |
-
#
|
| 22 |
-
# We use an alternative endpoint here to help when the primary model is overloaded.
|
| 23 |
model = HfApiModel(
|
| 24 |
-
max_tokens=2096, #
|
| 25 |
-
temperature=0.5, #
|
|
|
|
| 26 |
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
|
| 27 |
-
# (To
|
| 28 |
custom_role_conversions=None
|
| 29 |
)
|
| 30 |
|
| 31 |
# ------------------------------------------------------------------------------
|
| 32 |
# Define the simplify_text tool.
|
| 33 |
-
# This tool takes technical text and
|
| 34 |
-
# It
|
|
|
|
| 35 |
@tool
|
| 36 |
def simplify_text(text: str) -> str:
|
| 37 |
"""
|
| 38 |
Converts technical text into plain, everyday language.
|
| 39 |
-
It explains any technical terms
|
|
|
|
| 40 |
|
| 41 |
Args:
|
| 42 |
text: A technical sentence or paragraph.
|
| 43 |
|
| 44 |
Returns:
|
| 45 |
-
A simplified
|
| 46 |
"""
|
| 47 |
-
# Build
|
| 48 |
-
# 1. Convert the text into plain language.
|
| 49 |
-
# 2. Avoid technical jargon.
|
| 50 |
-
# 3. If technical terms are necessary, explain them in simple language.
|
| 51 |
prompt = (
|
| 52 |
-
"Please
|
| 53 |
-
"
|
| 54 |
-
"
|
| 55 |
f"Technical text:\n{text}\n\n"
|
| 56 |
"Simplified explanation:"
|
| 57 |
)
|
| 58 |
-
# Call the model with
|
| 59 |
response = model(prompt)
|
| 60 |
return response
|
| 61 |
|
| 62 |
# ------------------------------------------------------------------------------
|
| 63 |
-
# Load
|
| 64 |
-
# These
|
| 65 |
with open("prompts.yaml", 'r') as stream:
|
| 66 |
prompt_templates = yaml.safe_load(stream)
|
| 67 |
|
| 68 |
# ------------------------------------------------------------------------------
|
| 69 |
# Create our AI agent.
|
| 70 |
-
# The agent
|
| 71 |
-
#
|
|
|
|
|
|
|
| 72 |
agent = CodeAgent(
|
| 73 |
model=model,
|
| 74 |
tools=[
|
| 75 |
FinalAnswerTool(),
|
| 76 |
simplify_text
|
| 77 |
],
|
| 78 |
-
max_steps=6, # Limit the number of reasoning steps.
|
| 79 |
-
verbosity_level=1, #
|
| 80 |
prompt_templates=prompt_templates
|
| 81 |
)
|
| 82 |
|
| 83 |
# ------------------------------------------------------------------------------
|
| 84 |
# Launch the interactive user interface.
|
| 85 |
-
# GradioUI creates a webpage where users can
|
| 86 |
-
#
|
| 87 |
-
if
|
| 88 |
-
GradioUI(agent).launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
|
| 2 |
+
# Import necessary parts of our AI framework.
|
| 3 |
+
# CodeAgent helps us build our helper agent.
|
| 4 |
+
# HfApiModel sets up the model (the "brain") that processes text.
|
| 5 |
+
# The @tool decorator marks functions as "tools" that our agent can use.
|
| 6 |
from smolagents import CodeAgent, HfApiModel, tool
|
| 7 |
|
| 8 |
# ------------------------------------------------------------------------------
|
| 9 |
+
# Import YAML so we can load additional instructions from a file.
|
| 10 |
import yaml
|
| 11 |
|
| 12 |
# ------------------------------------------------------------------------------
|
| 13 |
+
# Import our helper tool for preparing the final answer and the user interface.
|
| 14 |
+
# FinalAnswerTool packages the final answer to show to the user.
|
| 15 |
# GradioUI creates a simple webpage for user interaction.
|
| 16 |
from tools.final_answer import FinalAnswerTool
|
| 17 |
from Gradio_UI import GradioUI
|
| 18 |
|
| 19 |
# ------------------------------------------------------------------------------
|
| 20 |
+
# Set up our AI model (the "brain" of our agent).
|
| 21 |
+
# This model processes text prompts and generates responses.
|
|
|
|
| 22 |
model = HfApiModel(
|
| 23 |
+
max_tokens=2096, # Maximum length of the generated answer.
|
| 24 |
+
temperature=0.5, # Controls how creative or focused the response is.
|
| 25 |
+
# We use an alternative model endpoint to help if the primary model is busy.
|
| 26 |
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
|
| 27 |
+
# (To revert to the original model, you could use: model_id='Qwen/Qwen2.5-Coder-32B-Instruct')
|
| 28 |
custom_role_conversions=None
|
| 29 |
)
|
| 30 |
|
| 31 |
# ------------------------------------------------------------------------------
|
| 32 |
# Define the simplify_text tool.
|
| 33 |
+
# This tool takes technical text and instructs the model to explain it in plain, everyday language.
|
| 34 |
+
# It tells the model to break complex ideas into simple sentences, and if any technical terms are necessary,
|
| 35 |
+
# to provide a brief definition in parentheses.
|
| 36 |
@tool
|
| 37 |
def simplify_text(text: str) -> str:
|
| 38 |
"""
|
| 39 |
Converts technical text into plain, everyday language.
|
| 40 |
+
It explains any technical terms (by providing a simple definition in parentheses)
|
| 41 |
+
and breaks down complex ideas into short, clear sentences.
|
| 42 |
|
| 43 |
Args:
|
| 44 |
text: A technical sentence or paragraph.
|
| 45 |
|
| 46 |
Returns:
|
| 47 |
+
A simplified explanation of the text.
|
| 48 |
"""
|
| 49 |
+
# Build an improved prompt with detailed instructions:
|
|
|
|
|
|
|
|
|
|
| 50 |
prompt = (
|
| 51 |
+
"Please read the following technical text and explain it in plain, everyday language as if you were talking to someone with little technical knowledge. "
|
| 52 |
+
"Break down complex ideas into simple, short sentences. If you need to use any technical terms, provide a brief definition in parentheses right after the term. "
|
| 53 |
+
"Do not use confusing jargon; instead, use simple words. \n\n"
|
| 54 |
f"Technical text:\n{text}\n\n"
|
| 55 |
"Simplified explanation:"
|
| 56 |
)
|
| 57 |
+
# Call the model with the prompt. The model should generate a simplified explanation.
|
| 58 |
response = model(prompt)
|
| 59 |
return response
|
| 60 |
|
| 61 |
# ------------------------------------------------------------------------------
|
| 62 |
+
# Load additional instructions from the 'prompts.yaml' file.
|
| 63 |
+
# These prompt templates help guide the model's behavior.
|
| 64 |
with open("prompts.yaml", 'r') as stream:
|
| 65 |
prompt_templates = yaml.safe_load(stream)
|
| 66 |
|
| 67 |
# ------------------------------------------------------------------------------
|
| 68 |
# Create our AI agent.
|
| 69 |
+
# The agent is built by combining the model with our tools.
|
| 70 |
+
# Here we include:
|
| 71 |
+
# - FinalAnswerTool: Packages the final answer.
|
| 72 |
+
# - simplify_text: Converts technical text into plain language.
|
| 73 |
agent = CodeAgent(
|
| 74 |
model=model,
|
| 75 |
tools=[
|
| 76 |
FinalAnswerTool(),
|
| 77 |
simplify_text
|
| 78 |
],
|
| 79 |
+
max_steps=6, # Limit the number of reasoning steps the agent can use.
|
| 80 |
+
verbosity_level=1, # A lower number keeps internal details minimal.
|
| 81 |
prompt_templates=prompt_templates
|
| 82 |
)
|
| 83 |
|
| 84 |
# ------------------------------------------------------------------------------
|
| 85 |
# Launch the interactive user interface.
|
| 86 |
+
# GradioUI creates a webpage where users can type in technical text and see the simplified version.
|
| 87 |
+
# The interface listens on all network addresses (0.0.0.0) on port 7860.
|
| 88 |
+
if __n
|
|
|