3

I'm trying to call the API to a chatbot I've created with Gradio library. When I check the API documents in gradio-client, it shows that:

from gradio_client import Client

client = Client("https://c88f6cc8edfb2f573b.gradio.live/")
result = client.predict(
                "Howdy!",   # str representing string value in 'parameter_12' Textbox component
                "null", # str representing filepath to JSON file in 'parameter_11' Chatbot component
                api_name="/chat"
)
print(result)

I'm confused about the second parameter (# str representing filepath to JSON file in 'parameter_11' Chatbot component). What does the JSON file of Gradio Chatbot look like? Can I get an example of using Gradio Chatbot through API?. Here is the code to generate chatbot in Gradio documents:

import gradio as gr
import random
import time

with gr.Blocks() as demo:
    chatbot = gr.Chatbot()
    msg = gr.Textbox()
    clear = gr.Button("Clear")

    def user(user_message, history):
        return "", history + [[user_message, None]]

    def bot(history):
        bot_message = random.choice(["Yes", "No"])
        history[-1][1] = bot_message
        time.sleep(1)
        return history

    msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False, api_name="chat").then(
        bot, chatbot, chatbot
    )
    clear.click(lambda: None, None, chatbot, queue=False, api_name="clear")

if __name__ == "__main__":
    demo.launch(share=True)

I've tried the code

from gradio_client import Client

client = Client("https://c88f6cc8edfb2f573b.gradio.live/")
result = client.predict(
                "hello",    # str representing string value in 'parameter_2' Textbox component
                "/content/chatbot.json",    # str representing filepath to JSON file in 'parameter_1' Chatbot component
                api_name="/chat"
)
print(result)

and got the error

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.9/dist-packages/gradio_client/client.py in _predict(*data)
    633             try:
--> 634                 output = result["data"]
    635             except KeyError:

KeyError: 'data'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
7 frames
/usr/local/lib/python3.9/dist-packages/gradio_client/client.py in _predict(*data)
    638                     and not huggingface_hub.space_info(self.client.space_id).private
    639                 )
--> 640                 if "error" in result and "429" in result["error"] and is_public_space:
    641                     raise utils.TooManyRequestsError(
    642                         f"Too many requests to the API, please try again later. To avoid being rate-limited, please duplicate the Space using Client.duplicate({self.client.space_id}) and pass in your Hugging Face token."

TypeError: argument of type 'NoneType' is not iterable
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • did you find the solution? I have the same problem – Eric Bellet Aug 03 '23 at 09:01
  • I created the file "/content/chatbot.json" with this content {"[('value','value2')]":"[('value','value2')]"} then I got this error gradio_client/client.py", line 681, in _predict raise ValueError(result["error"]) from None ValueError: Expected a list of lists or list of tuples. Received: [('value','value2')] – Eric Bellet Aug 03 '23 at 09:15
  • related? https://github.com/gradio-app/gradio/issues/4081 – starball Aug 06 '23 at 21:40

0 Answers0