Spaces:
Runtime error
Runtime error
Update app.py
Browse files181 unmodified lines1821831841851861871885 unmodified lines19419519619719819920020120220320420520620720820915 unmodified lines22522622722822923023123223323423523623723823924024124261 unmodified lines30430530630730830931015 unmodified lines326327328181 unmodified lines):"""Fetches jokes from JokeAPI. Use this tool when the user asks for a joke.**IMPORTANT:** For simple requests like "tell me a joke", you do not need to specify any parameters. The function will automatically fetch a safe, general joke by default.Args:5 unmodified linescontains (str | None): A string that MUST be present in the joke. Only use if the user asks for a joke containing a specific word.idRange (str | None): A specific ID or a range of IDs for jokes. Do not use unless specifically requested.amount (int): The number of jokes to fetch (1-10). Only change this if the user asks for more than one joke.Returns:The function's output is ALWAYS a list containing one or more jokes.- If one joke is requested (amount=1), it returns a list with a single element: [joke].- If multiple jokes are requested (amount > 1), it returns a list of jokes: [joke1, joke2, ...].Each 'joke' in the list will be one of two formats:- For a single-part joke: a simple string. (e.g., "Why don't scientists trust atoms? Because they make up everything!")- For a two-part joke: a dictionary with "setup" and "delivery" keys. (e.g., {"setup": "What do you call a fake noodle?", "delivery": "An Impasta."})"""# The 'format' parameter is hardcoded to 'json' as it's the only one we parse.# It's removed from the function signature to avoid user confusion in the UI.url = f"https://v2.jokeapi.dev/joke/{categories}"params: dict[str, str | int | bool] = {"format": "json", "lang": lang, "amount": amount}15 unmodified linesdata = resp.json()if data.get("error"):# Return the error message as a string in a list to be displayed in the JSON outputreturn [f"JokeAPI error: {data.get('message', 'Unknown error')}"]def extract(j: dict):return j["joke"] if j["type"] == "single" else {"setup": j["setup"], "delivery": j["delivery"]}# API returns a 'jokes' list for amount > 1, and a single object for amount == 1# This code normalizes the output to always be a list for consistency.if amount > 1:return [extract(j) for j in data.get("jokes", [])]else:return [extract(data)]except requests.exceptions.RequestException as e:return [f"Network error: {e}"]61 unmodified linesoutputs=gr.JSON(),api_name="joke_generation",title="Joke Generation Tool",description="Fetches jokes from JokeAPI using various filters.")# Define the interface for the Sentiment Analysis tool15 unmodified lines# Launch the interface and MCP serverif __name__ == "__main__":demo.launch(mcp_server=True)181 unmodified lines1821831841851861875 unmodified lines19319419519619719819920020120220320420520615 unmodified lines22222322422522622722822923023123223323423523623723823924024124224361 unmodified lines30530630730830931031115 unmodified lines327328329330181 unmodified lines):"""Fetches jokes from JokeAPI. Use this tool when the user asks for a joke.**IMPORTANT:** For simple requests like "tell me a joke", you do not need to specify any parameters. The function will automatically fetch a safe, general joke by default.Args:5 unmodified linescontains (str | None): A string that MUST be present in the joke. Only use if the user asks for a joke containing a specific word.idRange (str | None): A specific ID or a range of IDs for jokes. Do not use unless specifically requested.amount (int): The number of jokes to fetch (1-10). Only change this if the user asks for more than one joke.Returns:A list of joke strings. If amount=1, returns a list with a single string; if amount>1, returns multiple strings.**Example:**>>> joke_generation()["Why don't scientists trust atoms? Because they make up everything!"]"""url = f"https://v2.jokeapi.dev/joke/{categories}"params: dict[str, str | int | bool] = {"format": "json", "lang": lang, "amount": amount}15 unmodified linesdata = resp.json()if data.get("error"):return [f"JokeAPI error: {data.get('message', 'Unknown error')}" ]def extract(j: dict):return j["joke"] if j.get("type") == "single" else {"setup": j["setup"], "delivery": j["delivery"]}# Normalize output and flatten single-part dictionariesjokes = []if amount > 1:jokes = [extract(j) for j in data.get("jokes", [])]else:single = extract(data)if isinstance(single, dict):# flatten to a single stringsingle = f"{single['setup']} {single['delivery']}"jokes = [single]return jokesexcept requests.exceptions.RequestException as e:return [f"Network error: {e}"]61 unmodified linesoutputs=gr.JSON(),api_name="joke_generation",title="Joke Generation Tool",description="Fetches jokes from JokeAPI using various filters. For a simple joke, just click Submit without changing any fields.")# Define the interface for the Sentiment Analysis tool15 unmodified lines# Launch the interface and MCP serverif __name__ == "__main__":demo.launch(mcp_server=True)
app.py+19-17