Spaces:
Build error
Build error
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182# app.py# --------------------------------------------# Importing necessary parts of our AI framework.# These help us create our "agent" (our helpful assistant) and define "tools" (small functions that do specific tasks).from smolagents import CodeAgent, HfApiModel, tool# --------------------------------------------# Importing a library to work with YAML files.# YAML files are used to store settings and instructions in a simple text format.import yaml# --------------------------------------------# Importing our FinalAnswerTool and GradioUI.# FinalAnswerTool helps produce the final output that the user sees.# GradioUI sets up a simple webpage so users can talk to our agent.from tools.final_answer import FinalAnswerToolfrom Gradio_UI import GradioUI# ------------------------------------------------------------------------------------# Here we define a tool called simplify_text.# A "tool" is a small piece of code that performs one job.# In this case, the job is to take technical text and make it look simpler.@tooldef simplify_text(text: str) -> str:"""Simplifies a technical sentence or paragraph into plain language.Args:text: A technical sentence or paragraph.Returns:A simplified version of the text."""# This simple version just adds "Simplified:" before the original text.# Later, you can make this more advanced by changing the wording.return "Simplified: " + text# ------------------------------------------------------------------------------------# Now we set up the AI model.# Think of the model as the "brain" of our agent that understands questions and comes up with answers.# We use a model hosted at a specific web address.model = HfApiModel(max_tokens=2096, # This sets a limit on how long the model's answer can be.temperature=0.5, # This controls the creativity of the answer (0.5 is moderate).# We are using an alternative endpoint here. If the original model is overloaded,# you can use this endpoint instead.model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',# If you ever want to switch back to the original model, you could use:# model_id='Qwen/Qwen2.5-Coder-32B-Instruct',custom_role_conversions=None)# ------------------------------------------------------------------------------------# Next, we load some prompt templates from a file called "prompts.yaml".# Prompt templates act like a script that tells the model how to behave.with open("prompts.yaml", 'r') as stream:prompt_templates = yaml.safe_load(stream)# ------------------------------------------------------------------------------------# Here we create our AI agent.# The agent is like your digital helper that uses the model (its brain) and the tools (its abilities) to answer questions.# We give it two tools:# - FinalAnswerTool: This tool helps package the final answer.# - simplify_text: Our custom tool that makes technical language simpler.agent = CodeAgent(model=model,tools=[FinalAnswerTool(),simplify_text],max_steps=6, # This sets a limit on how many steps the agent can take to come up with an answer.verbosity_level=1, # This controls how detailed the agent's inner thoughts are (for debugging or learning).prompt_templates=prompt_templates)# ------------------------------------------------------------------------------------# Finally, we start the user interface so that people can interact with the agent.# Gradio makes a simple webpage where you can type questions and see answers.# The following line tells Gradio to listen on all network addresses (0.0.0.0) at port 7860.if __name__ == "__main__":GradioUI(agent).launch(server_name="0.0.0.0", server_port=7860)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081# Import necessary parts of our AI framework to create our agent and define our tools.from smolagents import CodeAgent, HfApiModel, tool# ------------------------------------------------------------------------------# Import YAML to load additional instructions from a file.import yaml# ------------------------------------------------------------------------------# Import our helper tool for producing the final answer and the user interface.from tools.final_answer import FinalAnswerToolfrom Gradio_UI import GradioUI# ------------------------------------------------------------------------------# Set up the AI model (the "brain" of our agent).# The model processes text based on prompts and produces answers.# We use an alternative endpoint if needed.model = HfApiModel(max_tokens=2096, # Maximum length of the answer.temperature=0.5, # Controls how creative the answer is.model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',# (You can switch to another model by changing the model_id.)custom_role_conversions=None)# ------------------------------------------------------------------------------# Define the simplify_text tool.# This tool takes technical text and asks the LLM to convert it into plain, simple language.# It also instructs the model to define any technical jargon in easy-to-understand terms.@tooldef simplify_text(text: str) -> str:"""Converts technical text into plain language, avoiding jargon and explaining any technical terms.Args:text: A technical sentence or paragraph.Returns:A simplified version of the text in common language."""# Create a prompt that instructs the model to rephrase the text.# The prompt tells the model:# - "Convert this technical text into simple, common language."# - "Avoid using technical jargon, and if you use any, explain it in plain language."prompt = ("Convert the following technical text into plain, common language. Avoid technical jargon. ""If you must use technical terms, please define them in simple language:\n\n"f"{text}\n\nSimplified version:")# Call the model with our prompt and store its response.response = model(prompt)# Return the response, which should be a simplified version of the input text.return response# ------------------------------------------------------------------------------# Load prompt templates from the 'prompts.yaml' file.# These templates provide extra instructions to help guide the model.with open("prompts.yaml", 'r') as stream:prompt_templates = yaml.safe_load(stream)# ------------------------------------------------------------------------------# Create our AI agent by combining the model with our tools.# Our agent uses the FinalAnswerTool (to package the final answer)# and the simplify_text tool (to rephrase technical text into simple language).agent = CodeAgent(model=model,tools=[FinalAnswerTool(),simplify_text],max_steps=6, # Limit the number of steps the agent can use to produce an answer.verbosity_level=1, # Lower verbosity for a cleaner output.prompt_templates=prompt_templates)# ------------------------------------------------------------------------------# Launch the interactive user interface.# Gradio creates a simple webpage for users to type in their text and see the agent's answer.# The interface listens on all network addresses (0.0.0.0) on port 7860.if __name__ == "__main__":GradioUI(agent).launch(server_name="0.0.0.0", server_port=7860)
app.py+50-51