Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,42 @@
|
|
| 1 |
|
| 2 |
-
# Import necessary parts of our AI framework.
|
|
|
|
|
|
|
|
|
|
| 3 |
from smolagents import CodeAgent, HfApiModel, tool
|
| 4 |
|
| 5 |
# ------------------------------------------------------------------------------
|
| 6 |
-
# Import YAML
|
| 7 |
import yaml
|
| 8 |
|
| 9 |
# ------------------------------------------------------------------------------
|
| 10 |
-
# Import our helper tool for
|
|
|
|
|
|
|
| 11 |
from tools.final_answer import FinalAnswerTool
|
| 12 |
from Gradio_UI import GradioUI
|
| 13 |
|
| 14 |
# ------------------------------------------------------------------------------
|
| 15 |
-
# Set up
|
|
|
|
|
|
|
| 16 |
model = HfApiModel(
|
| 17 |
-
max_tokens=2096, #
|
| 18 |
-
temperature=0.5, #
|
| 19 |
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
|
|
|
|
| 20 |
custom_role_conversions=None
|
| 21 |
)
|
| 22 |
|
| 23 |
# ------------------------------------------------------------------------------
|
| 24 |
# Define the simplify_text tool.
|
| 25 |
-
# This tool
|
| 26 |
-
#
|
| 27 |
@tool
|
| 28 |
def simplify_text(text: str) -> str:
|
| 29 |
"""
|
| 30 |
Converts technical text into plain, everyday language.
|
| 31 |
-
|
| 32 |
|
| 33 |
Args:
|
| 34 |
text: A technical sentence or paragraph.
|
|
@@ -36,37 +44,45 @@ def simplify_text(text: str) -> str:
|
|
| 36 |
Returns:
|
| 37 |
A simplified version of the text.
|
| 38 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
prompt = (
|
| 40 |
-
"Please
|
| 41 |
-
"
|
| 42 |
-
"
|
| 43 |
f"Technical text:\n{text}\n\n"
|
| 44 |
"Simplified explanation:"
|
| 45 |
)
|
|
|
|
| 46 |
response = model(prompt)
|
| 47 |
return response
|
| 48 |
|
| 49 |
# ------------------------------------------------------------------------------
|
| 50 |
-
# Load
|
|
|
|
| 51 |
with open("prompts.yaml", 'r') as stream:
|
| 52 |
prompt_templates = yaml.safe_load(stream)
|
| 53 |
|
| 54 |
# ------------------------------------------------------------------------------
|
| 55 |
-
# Create our AI agent
|
| 56 |
-
#
|
| 57 |
-
#
|
| 58 |
agent = CodeAgent(
|
| 59 |
model=model,
|
| 60 |
tools=[
|
| 61 |
FinalAnswerTool(),
|
| 62 |
simplify_text
|
| 63 |
],
|
| 64 |
-
max_steps=6,
|
| 65 |
-
verbosity_level=1,
|
| 66 |
prompt_templates=prompt_templates
|
| 67 |
)
|
| 68 |
|
| 69 |
# ------------------------------------------------------------------------------
|
| 70 |
-
# Launch the interactive
|
|
|
|
|
|
|
| 71 |
if __name__ == "__main__":
|
| 72 |
GradioUI(agent).launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
|
| 2 |
+
# Import the necessary parts of our AI framework.
|
| 3 |
+
# CodeAgent helps us create our helper agent.
|
| 4 |
+
# HfApiModel is used to set up the model (the brain) that processes prompts.
|
| 5 |
+
# The @tool decorator marks functions as "tools" the agent can use.
|
| 6 |
from smolagents import CodeAgent, HfApiModel, tool
|
| 7 |
|
| 8 |
# ------------------------------------------------------------------------------
|
| 9 |
+
# Import YAML so we can load additional instructions from a YAML file.
|
| 10 |
import yaml
|
| 11 |
|
| 12 |
# ------------------------------------------------------------------------------
|
| 13 |
+
# Import our helper tool for preparing the final output and the user interface.
|
| 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 the agent).
|
| 21 |
+
# The model processes text prompts and returns answers.
|
| 22 |
+
# We use an alternative endpoint here to help when the primary model is overloaded.
|
| 23 |
model = HfApiModel(
|
| 24 |
+
max_tokens=2096, # This sets the maximum length of the answer.
|
| 25 |
+
temperature=0.5, # This controls how creative the answer is.
|
| 26 |
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
|
| 27 |
+
# (To switch back to the original model, 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 asks the LLM to convert it into plain language.
|
| 34 |
+
# It also instructs the model to explain any technical jargon.
|
| 35 |
@tool
|
| 36 |
def simplify_text(text: str) -> str:
|
| 37 |
"""
|
| 38 |
Converts technical text into plain, everyday language.
|
| 39 |
+
It explains any technical terms in simple language for someone with little tech knowledge.
|
| 40 |
|
| 41 |
Args:
|
| 42 |
text: A technical sentence or paragraph.
|
|
|
|
| 44 |
Returns:
|
| 45 |
A simplified version of the text.
|
| 46 |
"""
|
| 47 |
+
# Build a clear prompt that instructs the model on what to do:
|
| 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 convert the following technical text into plain, everyday language. "
|
| 53 |
+
"Avoid using technical jargon, and if you must use any, explain them in simple words. "
|
| 54 |
+
"Break down complex ideas into simple, short sentences:\n\n"
|
| 55 |
f"Technical text:\n{text}\n\n"
|
| 56 |
"Simplified explanation:"
|
| 57 |
)
|
| 58 |
+
# Call the model with our prompt. The model's response should be the simplified text.
|
| 59 |
response = model(prompt)
|
| 60 |
return response
|
| 61 |
|
| 62 |
# ------------------------------------------------------------------------------
|
| 63 |
+
# Load extra instructions from the 'prompts.yaml' file.
|
| 64 |
+
# These instructions help guide the model's behavior.
|
| 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 uses the model (its brain) and the tools we've defined to generate answers.
|
| 71 |
+
# We include the FinalAnswerTool to package the answer and our simplify_text tool.
|
| 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, # Lower verbosity for cleaner output.
|
| 80 |
prompt_templates=prompt_templates
|
| 81 |
)
|
| 82 |
|
| 83 |
# ------------------------------------------------------------------------------
|
| 84 |
+
# Launch the interactive user interface.
|
| 85 |
+
# GradioUI creates a webpage where users can enter text and see the agent's answer.
|
| 86 |
+
# It listens on all network interfaces (0.0.0.0) on port 7860.
|
| 87 |
if __name__ == "__main__":
|
| 88 |
GradioUI(agent).launch(server_name="0.0.0.0", server_port=7860)
|