Repository: gradio-app/gradio

Issue:
Add conversational pipeline to Interface.load
- [X] I have searched to see if a similar issue already exists.

When trying to use the HuggingFace hosted Chatbot-models the Interface.load fails with the error:
`ValueError: Unsupported pipeline type: conversational`

The hosted models on HuggingFace are really useful for testing out models without having to download them. And also makes it possible to use large models while developing on a laptop.

So it would be very useful to be able to use conversational models via the HTTP load functionality.



Discussion:

Resolving patch:
diff --git a/gradio/external.py b/gradio/external.py
--- a/gradio/external.py
+++ b/gradio/external.py
@@ -63,6 +63,32 @@ def load_blocks_from_repo(
     return blocks
 
 
+def chatbot_preprocess(text, state):
+    payload = {
+        "inputs": {"generated_responses": None, "past_user_inputs": None, "text": text}
+    }
+    if state is not None:
+        payload["inputs"]["generated_responses"] = state["conversation"][
+            "generated_responses"
+        ]
+        payload["inputs"]["past_user_inputs"] = state["conversation"][
+            "past_user_inputs"
+        ]
+
+    return payload
+
+
+def chatbot_postprocess(response):
+    response_json = response.json()
+    chatbot_value = list(
+        zip(
+            response_json["conversation"]["past_user_inputs"],
+            response_json["conversation"]["generated_responses"],
+        )
+    )
+    return chatbot_value, response_json
+
+
 def from_model(model_name: str, api_key: str | None, alias: str | None, **kwargs):
     model_url = "https://huggingface.co/{}".format(model_name)
     api_url = "https://api-inference.huggingface.co/models/{}".format(model_name)
@@ -76,7 +102,6 @@ def from_model(model_name: str, api_key: str | None, alias: str | None, **kwargs
         response.status_code == 200
     ), f"Could not find model: {model_name}. If it is a private or gated model, please provide your Hugging Face access token (https://huggingface.co/settings/tokens) as the argument for the `api_key` parameter."
     p = response.json().get("pipeline_tag")
-
     pipelines = {
         "audio-classification": {
             # example model: ehcalabres/wav2vec2-lg-xlsr-en-speech-emotion-recognition
@@ -101,6 +126,12 @@ def from_model(model_name: str, api_key: str | None, alias: str | None, **kwargs
             "preprocess": to_binary,
             "postprocess": lambda r: r.json()["text"],
         },
+        "conversational": {
+            "inputs": [components.Textbox(), components.State()],  # type: ignore
+            "outputs": [components.Chatbot(), components.State()],  # type: ignore
+            "preprocess": chatbot_preprocess,
+            "postprocess": chatbot_postprocess,
+        },
         "feature-extraction": {
             # example model: julien-c/distilbert-feature-extraction
             "inputs": components.Textbox(label="Input"),
@@ -125,6 +156,12 @@ def from_model(model_name: str, api_key: str | None, alias: str | None, **kwargs
                 {i["label"].split(", ")[0]: i["score"] for i in r.json()}
             ),
         },
+        "image-to-text": {
+            "inputs": components.Image(type="filepath", label="Input Image"),
+            "outputs": components.Textbox(),
+            "preprocess": to_binary,
+            "postprocess": lambda r: r.json()[0]["generated_text"],
+        },
         "question-answering": {
             # Example: deepset/xlm-roberta-base-squad2
             "inputs": [
@@ -311,7 +348,12 @@ def query_huggingface_api(*params):
     }
 
     kwargs = dict(interface_info, **kwargs)
-    kwargs["_api_mode"] = True  # So interface doesn't run pre/postprocess.
+
+    # So interface doesn't run pre/postprocess
+    # except for conversational interfaces which
+    # are stateful
+    kwargs["_api_mode"] = p != "conversational"
+
     interface = gradio.Interface(**kwargs)
     return interface
 


Test patch:
diff --git a/test/test_external.py b/test/test_external.py
--- a/test/test_external.py
+++ b/test/test_external.py
@@ -228,6 +228,28 @@ def test_numerical_to_label_space(self):
         except TooManyRequestsError:
             pass
 
+    def test_image_to_text(self):
+        io = gr.Interface.load("models/nlpconnect/vit-gpt2-image-captioning")
+        try:
+            output = io("gradio/test_data/lion.jpg")
+            assert isinstance(output, str)
+        except TooManyRequestsError:
+            pass
+
+    def test_conversational(self):
+        io = gr.Interface.load("models/microsoft/DialoGPT-medium")
+        app, _, _ = io.launch(prevent_thread_lock=True)
+        client = TestClient(app)
+        assert app.state_holder == {}
+        response = client.post(
+            "/api/predict/",
+            json={"session_hash": "foo", "data": ["Hi!", None], "fn_index": 0},
+        )
+        output = response.json()
+        assert isinstance(output["data"], list)
+        assert isinstance(output["data"][0], list)
+        assert isinstance(app.state_holder["foo"], dict)
+
     def test_speech_recognition_model(self):
         io = gr.Interface.load("models/facebook/wav2vec2-base-960h")
         try: