emiliamzz's picture
Update app.py
0ab259d verified
import datetime
import os
import pytz
import requests
import yaml
from huggingface_hub import InferenceClient
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
from tools.final_answer import FinalAnswerTool
from tools.visit_webpage import VisitWebpageTool
from tools.web_search import DuckDuckGoSearchTool
from transformers import pipeline, AutoModelForCausalLM
from Gradio_UI import GradioUI
from PIL.PngImagePlugin import PngImageFile
client = InferenceClient(
provider="auto",
api_key=os.environ["HF_TOKEN"],
)
@tool
def document_qa(document: str, question: str)-> str:
"""A tool that finds the answer to a question in a specified document.
Args:
document: A string representing the URL to the document. The document can be any file type, including PDF, PNG, and JPEG.
question: A string representing the question being asked.
"""
pipe = pipeline("document-question-answering", model="impira/layoutlm-document-qa",)
answer = pipe(document, question)
return answer[0]['answer']
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""A tool that fetches the current local time in a specified timezone.
Args:
timezone: A string representing a valid timezone (e.g., 'America/New_York').
"""
try:
# Create timezone object
tz = pytz.timezone(timezone)
# Get current time in that timezone
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
return f"The current local time in {timezone} is: {local_time}"
except Exception as e:
return f"Error fetching time for timezone '{timezone}': {str(e)}"
@tool
def image_generator(prompt: str) -> PngImageFile:
"""A tool that generates an image based on a specified description.
Args:
prompt: A string describing the image to generate.
"""
image = client.text_to_image(
prompt,
model="ByteDance/SDXL-Lightning",
)
return image
@tool
def image_qa(image: str, question: str) -> str:
"""A tool that answers a question based on a specified image.
Args:
image: A string representing the URL to the image.
question: A string representing the question being asked.
"""
pipe = pipeline("visual-question-answering", model="dandelin/vilt-b32-finetuned-vqa")
answer = pipe(image, question)
return answer[0]['answer']
@tool
def translator(text: str, src_lang: str, tgt_lang: str) -> str:
"""A tool that translates text from a specified language to another specified language.
Args:
text: A string to translate.
src_lang: A string representation of the source language code (e.g., 'fr'). Must be two characters long and in lowercase.
tgt_lang: A string representation of the target language code (e.g., 'en'). Must be two characters long and in lowercase.
"""
model = "Helsinki-NLP/opus-mt-" + src_lang + "-" + tgt_lang
result = client.translation(
text,
model=model,
)
return result.translation_text
final_answer = FinalAnswerTool()
visit_webpage = VisitWebpageTool()
web_search = DuckDuckGoSearchTool()
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
model = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
custom_role_conversions=None,
)
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
agent = CodeAgent(
model=model,
tools=[document_qa, final_answer, image_generator, image_qa, translator, visit_webpage, web_search],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
GradioUI(agent).launch()