gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
29.49k stars 2.19k forks source link

Not able to call api function in custom chatbot #8183

Closed suchitra-30 closed 2 weeks ago

suchitra-30 commented 2 weeks ago

Describe the bug

So basically i have made custom chatbot in which i have just added two input and one output but then it shows clear button and submit button default . Now i want to call my delete api on clear button click i have tried many ways but it will show me extra clear button which does the functionality but there default clear button is not working how can i use that to call my function here is the screenshot - Screenshot from 2024-05-01 15-28-24

code - import gradio as gr import requests

API_KEY = "sec_jIePzZH" UPLOAD_URL = "https://api.chatpdf.com/v1/sources/add-file" CHAT_URL = "https://api.chatpdf.com/v1/chats/message"

def upload_pdf(pdf_file): files = {'file': ('file.pdf', pdf_file, 'application/octet-stream')} headers = {'x-api-key': API_KEY} response = requests.post(UPLOAD_URL, headers=headers, files=files) print(response.json()) if response.status_code == 200: print(response.json()["sourceId"]) return response.json()["sourceId"] else: return None

Function to send message to PDF

def send_message(source_id, message, include_references=False):

payload = {

#     "sourceId": source_id,
#     "messages": [{"role": "user", "content": message}]
# }
data = {
    'sourceId': source_id,
    'messages': [
        {
            "role": "user",
            "content": message,
        }
    ]
}

headers = {"x-api-key": API_KEY, "Content-Type": "application/json"}
# print(data)
try:
    response = requests.post('https://api.chatpdf.com/v1/chats/message', headers=headers, json=data)
    if response.status_code == 200:
        print(response.json()['content'])
        return response.json().get("content", "No response content available")
    else:
        return f"Failed to send message. Status: {response.status_code}, Error: {response.text}"
except requests.exceptions.RequestException as e:
    return f"Request Error: {e}"

Define the callback function for the chatbot

def chat_with_pdf(text, pdf_file, include_references=False):

Check if PDF file is provided

if pdf_file is not None:
    # Upload the PDF file to ChatPDF API
    source_id = upload_pdf(pdf_file)
    if source_id:
        # Send message to the PDF
        return send_message(source_id, text, include_references)
    else:
        return "Failed to upload PDF file"
else:
    return "PDF file not provided"

text_input = gr.Textbox("text", label="Enter your message here") file_input = gr.File(label="Upload PDF", type="binary")

with gr.Blocks() as chatbot_interface: chatbot = gr.Interface( fn=chat_with_pdf, inputs=[text_input, file_input], outputs="text", title="Multimodal Chatbot: PDF & Text", )

def delete_function():
    print("Delete API function called")

clear_button = gr.ClearButton([text_input, file_input])

clear_button.click(delete_function)

chatbot_interface.launch()

print("Chatbot interface launched successfully!")

Have you searched existing issues? 🔎

Reproduction

import gradio as gr

Screenshot

Screenshot from 2024-05-01 15-28-24

Logs

No response

System Info

Name: gradio
Version: 4.28.3

Severity

I can work around it

abidlabs commented 2 weeks ago

Hi @suchitra-30 can you please properly format the code in your issue by wrapping it inside triple backslashes and properly indenting so that we can try to reproduce the issue?

suchitra-30 commented 2 weeks ago

I have added in notebook with format - https://notepad.pw/XlR7rvKuGuSCRZFdsrEV, please check @abidlabs as in comment box its removing indentation

abidlabs commented 2 weeks ago

You have included private API keys, I highly encourage you to remove them if they are valid!

suchitra-30 commented 2 weeks ago

Its not valid still i have removed them with dummy value "xxxxxxxxxxxxx"

freddyaboulton commented 2 weeks ago

Hi @suchitra-30 ! The Cancel button that comes with gr.Interface is only meant to clear the UI. If you want to add custom functionality, I suggest you create your demo with the Blocks api and create a clear button that does what you would like!

suchitra-30 commented 2 weeks ago

@freddyaboulton, thank you for your guidance. I was wondering if there's a way to remove the default clear button from my code. Is that a possibility? Also, could you please share any blogs or resources that explain how to integrate a blog API in response to a chatbot?

freddyaboulton commented 2 weeks ago

Hi @suchitra-30 !

There isn't a way to remove the clear button from an interface I believe. So you would have to use the Blocks interface.

There is a series of guides in the gradio website about building chatbots. I recommend them! Here is the first one.

https://www.gradio.app/guides/creating-a-chatbot-fast

freddyaboulton commented 2 weeks ago

There are also ready-to-use templates in huggingface spaces for building chatbot demos that interface with the LLM via API.

image