mwill-AImission commited on
Commit
352856a
·
verified ·
1 Parent(s): 018cb2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -20
app.py CHANGED
@@ -1,42 +1,87 @@
1
- # app.py
2
 
3
- # ------------------------------------------------------------------------------
4
- # Import necessary parts of our AI framework.
5
- # CodeAgent helps us build our helper agent.
6
- # HfApiModel sets up the model (the "brain") that processes text.
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 additional instructions from a file.
12
  import yaml
13
 
14
  # ------------------------------------------------------------------------------
15
- # Import our helper tool for preparing the final answer and the user interface.
16
- # FinalAnswerTool packages the final answer to show to the user.
17
- # GradioUI creates a simple webpage for user interaction.
18
  from tools.final_answer import FinalAnswerTool
19
  from Gradio_UI import GradioUI
20
 
21
  # ------------------------------------------------------------------------------
22
- # Set up our AI model (the "brain" of our agent).
23
- # The model processes text prompts and generates responses.
24
  model = HfApiModel(
25
- max_tokens=2096, # Maximum length of the generated answer.
26
- temperature=0.5, # Controls how creative or focused the response is.
27
- # We use an alternative model endpoint if the primary model is busy.
28
  model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
29
- # (To revert to the original model, use: model_id='Qwen/Qwen2.5-Coder-32B-Instruct')
30
  custom_role_conversions=None
31
  )
32
 
33
  # ------------------------------------------------------------------------------
34
  # Define the simplify_text tool.
35
- # This tool takes technical text and instructs the LLM to explain it in plain language.
36
- # It tells the model to break down complex ideas into simple sentences and explain technical terms in simple words.
37
  @tool
38
  def simplify_text(text: str) -> str:
39
  """
40
  Converts technical text into plain, everyday language.
41
- It explains any technical terms (by providing a simple definition in parentheses)
42
- and breaks down complex ideas into short, clear senten
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)