Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,87 @@
|
|
| 1 |
-
# app.py
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
#
|
| 5 |
-
#
|
| 6 |
-
#
|
| 7 |
-
# The @tool decorator marks functions as "tools" that our agent can use.
|
| 8 |
from smolagents import CodeAgent, HfApiModel, tool
|
| 9 |
|
| 10 |
# ------------------------------------------------------------------------------
|
| 11 |
-
# Import YAML to load
|
| 12 |
import yaml
|
| 13 |
|
| 14 |
# ------------------------------------------------------------------------------
|
| 15 |
-
# Import our helper
|
| 16 |
-
# FinalAnswerTool
|
| 17 |
-
# GradioUI
|
| 18 |
from tools.final_answer import FinalAnswerTool
|
| 19 |
from Gradio_UI import GradioUI
|
| 20 |
|
| 21 |
# ------------------------------------------------------------------------------
|
| 22 |
-
# Set up our AI model
|
| 23 |
-
# The model processes text
|
| 24 |
model = HfApiModel(
|
| 25 |
-
max_tokens=2096,
|
| 26 |
-
temperature=0.5,
|
| 27 |
-
#
|
| 28 |
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
|
| 29 |
-
# (To
|
| 30 |
custom_role_conversions=None
|
| 31 |
)
|
| 32 |
|
| 33 |
# ------------------------------------------------------------------------------
|
| 34 |
# Define the simplify_text tool.
|
| 35 |
-
# This tool takes technical text and
|
| 36 |
-
#
|
| 37 |
@tool
|
| 38 |
def simplify_text(text: str) -> str:
|
| 39 |
"""
|
| 40 |
Converts technical text into plain, everyday language.
|
| 41 |
-
It explains
|
| 42 |
-
and breaks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
# Import the parts of the AI framework we need.
|
| 3 |
+
# - CodeAgent: Helps build our agent (the digital helper).
|
| 4 |
+
# - HfApiModel: Sets up the model (the "brain") that processes text.
|
| 5 |
+
# - @tool: Marks functions as "tools" our agent can use.
|
|
|
|
| 6 |
from smolagents import CodeAgent, HfApiModel, tool
|
| 7 |
|
| 8 |
# ------------------------------------------------------------------------------
|
| 9 |
+
# Import YAML to load extra instructions from a file.
|
| 10 |
import yaml
|
| 11 |
|
| 12 |
# ------------------------------------------------------------------------------
|
| 13 |
+
# Import our helper modules.
|
| 14 |
+
# - FinalAnswerTool: Packages the final answer for the user.
|
| 15 |
+
# - GradioUI: Creates a simple webpage so users can interact with our agent.
|
| 16 |
from tools.final_answer import FinalAnswerTool
|
| 17 |
from Gradio_UI import GradioUI
|
| 18 |
|
| 19 |
# ------------------------------------------------------------------------------
|
| 20 |
+
# Set up our AI model.
|
| 21 |
+
# The model processes text and returns answers.
|
| 22 |
model = HfApiModel(
|
| 23 |
+
max_tokens=2096, # Maximum length of the answer.
|
| 24 |
+
temperature=0.5, # How creative or focused the answer is.
|
| 25 |
+
# Use this endpoint as our model's address.
|
| 26 |
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
|
| 27 |
+
# (To use the original model instead, change the model_id to '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 model to convert it into plain language.
|
| 34 |
+
# The prompt instructs the model to break down complex ideas into short sentences and to define any technical terms in simple words.
|
| 35 |
@tool
|
| 36 |
def simplify_text(text: str) -> str:
|
| 37 |
"""
|
| 38 |
Converts technical text into plain, everyday language.
|
| 39 |
+
It explains technical terms using simple definitions (in parentheses)
|
| 40 |
+
and breaks complex ideas into short, clear sentences.
|
| 41 |
+
|
| 42 |
+
Args:
|
| 43 |
+
text: A technical sentence or paragraph.
|
| 44 |
+
|
| 45 |
+
Returns:
|
| 46 |
+
A simplified explanation of the text.
|
| 47 |
+
"""
|
| 48 |
+
# Build a detailed prompt for the model:
|
| 49 |
+
prompt = (
|
| 50 |
+
"Please convert the following technical text into plain, everyday language as if you were talking to someone with little technical knowledge. "
|
| 51 |
+
"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. "
|
| 52 |
+
"Do not use confusing jargon; instead, use simple words. \n\n"
|
| 53 |
+
f"Technical text:\n{text}\n\n"
|
| 54 |
+
"Simplified explanation:"
|
| 55 |
+
)
|
| 56 |
+
# Call the model with our prompt and get its response.
|
| 57 |
+
response = model(prompt)
|
| 58 |
+
return response
|
| 59 |
+
|
| 60 |
+
# ------------------------------------------------------------------------------
|
| 61 |
+
# Load additional instructions from the prompts.yaml file.
|
| 62 |
+
# This file contains extra guidance for how the model should behave.
|
| 63 |
+
with open("prompts.yaml", 'r') as stream:
|
| 64 |
+
prompt_templates = yaml.safe_load(stream)
|
| 65 |
+
|
| 66 |
+
# ------------------------------------------------------------------------------
|
| 67 |
+
# Create our AI agent by combining the model and our tools.
|
| 68 |
+
# Our agent uses:
|
| 69 |
+
# - FinalAnswerTool to package the final answer.
|
| 70 |
+
# - simplify_text to convert technical text into plain language.
|
| 71 |
+
agent = CodeAgent(
|
| 72 |
+
model=model,
|
| 73 |
+
tools=[
|
| 74 |
+
FinalAnswerTool(),
|
| 75 |
+
simplify_text
|
| 76 |
+
],
|
| 77 |
+
max_steps=6, # Limit the number of steps in reasoning.
|
| 78 |
+
verbosity_level=1, # Keep internal debug details minimal.
|
| 79 |
+
prompt_templates=prompt_templates
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# ------------------------------------------------------------------------------
|
| 83 |
+
# Launch the interactive user interface.
|
| 84 |
+
# GradioUI creates a webpage so users can type in technical text and see the simplified version.
|
| 85 |
+
# The launch command includes share=True so the app remains active with a public URL.
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
GradioUI(agent).launch(server_name="0.0.0.0", server_port=7860, share=True)
|