mwill-AImission commited on
Commit
bfdbdbc
·
verified ·
1 Parent(s): e34cf88

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -35
app.py CHANGED
@@ -1,88 +1,88 @@
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.
43
 
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)
 
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