Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
|
| 2 |
-
# Import necessary parts of our AI framework
|
| 3 |
from smolagents import CodeAgent, HfApiModel, tool
|
| 4 |
|
| 5 |
# ------------------------------------------------------------------------------
|
|
@@ -13,69 +13,60 @@ from Gradio_UI import GradioUI
|
|
| 13 |
|
| 14 |
# ------------------------------------------------------------------------------
|
| 15 |
# Set up the AI model (the "brain" of our agent).
|
| 16 |
-
# The model processes text based on prompts and produces answers.
|
| 17 |
-
# We use an alternative endpoint if needed.
|
| 18 |
model = HfApiModel(
|
| 19 |
max_tokens=2096, # Maximum length of the answer.
|
| 20 |
-
temperature=0.5, # Controls
|
| 21 |
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
|
| 22 |
-
# (You can switch to another model by changing the model_id.)
|
| 23 |
custom_role_conversions=None
|
| 24 |
)
|
| 25 |
|
| 26 |
# ------------------------------------------------------------------------------
|
| 27 |
# Define the simplify_text tool.
|
| 28 |
-
# This tool
|
| 29 |
-
#
|
| 30 |
@tool
|
| 31 |
def simplify_text(text: str) -> str:
|
| 32 |
"""
|
| 33 |
-
Converts technical text into plain
|
|
|
|
| 34 |
|
| 35 |
Args:
|
| 36 |
text: A technical sentence or paragraph.
|
| 37 |
|
| 38 |
Returns:
|
| 39 |
-
A simplified version of the text
|
| 40 |
"""
|
| 41 |
-
# Create a prompt that instructs the model to rephrase the text.
|
| 42 |
-
# The prompt tells the model:
|
| 43 |
-
# - "Convert this technical text into simple, common language."
|
| 44 |
-
# - "Avoid using technical jargon, and if you use any, explain it in plain language."
|
| 45 |
prompt = (
|
| 46 |
-
"
|
| 47 |
-
"
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
)
|
| 50 |
-
# Call the model with our prompt and store its response.
|
| 51 |
response = model(prompt)
|
| 52 |
-
# Return the response, which should be a simplified version of the input text.
|
| 53 |
return response
|
| 54 |
|
| 55 |
# ------------------------------------------------------------------------------
|
| 56 |
-
# Load prompt templates from
|
| 57 |
-
# These templates provide extra instructions to help guide the model.
|
| 58 |
with open("prompts.yaml", 'r') as stream:
|
| 59 |
prompt_templates = yaml.safe_load(stream)
|
| 60 |
|
| 61 |
# ------------------------------------------------------------------------------
|
| 62 |
-
# Create our AI agent
|
| 63 |
-
#
|
| 64 |
-
#
|
| 65 |
agent = CodeAgent(
|
| 66 |
model=model,
|
| 67 |
tools=[
|
| 68 |
FinalAnswerTool(),
|
| 69 |
simplify_text
|
| 70 |
],
|
| 71 |
-
max_steps=6,
|
| 72 |
-
verbosity_level=1,
|
| 73 |
prompt_templates=prompt_templates
|
| 74 |
)
|
| 75 |
|
| 76 |
# ------------------------------------------------------------------------------
|
| 77 |
-
# Launch the interactive
|
| 78 |
-
# Gradio creates a simple webpage for users to type in their text and see the agent's answer.
|
| 79 |
-
# The interface listens on all network addresses (0.0.0.0) on port 7860.
|
| 80 |
if __name__ == "__main__":
|
| 81 |
GradioUI(agent).launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
|
| 2 |
+
# Import necessary parts of our AI framework.
|
| 3 |
from smolagents import CodeAgent, HfApiModel, tool
|
| 4 |
|
| 5 |
# ------------------------------------------------------------------------------
|
|
|
|
| 13 |
|
| 14 |
# ------------------------------------------------------------------------------
|
| 15 |
# Set up the AI model (the "brain" of our agent).
|
|
|
|
|
|
|
| 16 |
model = HfApiModel(
|
| 17 |
max_tokens=2096, # Maximum length of the answer.
|
| 18 |
+
temperature=0.5, # Controls creativity.
|
| 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 now asks the model to convert technical text into plain language,
|
| 26 |
+
# and to explain any technical terms in a way that a non-expert would understand.
|
| 27 |
@tool
|
| 28 |
def simplify_text(text: str) -> str:
|
| 29 |
"""
|
| 30 |
+
Converts technical text into plain, everyday language.
|
| 31 |
+
Explains technical terms in simple language as if explaining to someone with little tech knowledge.
|
| 32 |
|
| 33 |
Args:
|
| 34 |
text: A technical sentence or paragraph.
|
| 35 |
|
| 36 |
Returns:
|
| 37 |
+
A simplified version of the text.
|
| 38 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
prompt = (
|
| 40 |
+
"Please read the following technical text and convert it into plain, everyday language. "
|
| 41 |
+
"Break down any complex ideas and explain any technical terms as if you were talking to someone "
|
| 42 |
+
"who has little knowledge about technology. Use simple words and short sentences.\n\n"
|
| 43 |
+
f"Technical text:\n{text}\n\n"
|
| 44 |
+
"Simplified explanation:"
|
| 45 |
)
|
|
|
|
| 46 |
response = model(prompt)
|
|
|
|
| 47 |
return response
|
| 48 |
|
| 49 |
# ------------------------------------------------------------------------------
|
| 50 |
+
# Load prompt templates from a YAML file.
|
|
|
|
| 51 |
with open("prompts.yaml", 'r') as stream:
|
| 52 |
prompt_templates = yaml.safe_load(stream)
|
| 53 |
|
| 54 |
# ------------------------------------------------------------------------------
|
| 55 |
+
# Create our AI agent using our model and our two tools:
|
| 56 |
+
# - FinalAnswerTool: Packages the final output.
|
| 57 |
+
# - simplify_text: Converts technical text into plain language.
|
| 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 UI so users can interact with the agent.
|
|
|
|
|
|
|
| 71 |
if __name__ == "__main__":
|
| 72 |
GradioUI(agent).launch(server_name="0.0.0.0", server_port=7860)
|