choupijiang / textbox_with_upload.py
luminoussg's picture
Create textbox_with_upload.py
d89084e verified
raw
history blame
944 Bytes
import gradio as gr
from gradio.components import Textbox
from gradio.components.base import Component
class TextboxWithUpload(Textbox):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.upload_button = None
def add_upload_button(self):
self.upload_button = gr.UploadButton("πŸ“Ž", file_types=["text"], file_count="single")
self.upload_button.upload(self.handle_upload, inputs=[self.upload_button], outputs=[self])
def handle_upload(self, file):
if file:
return file.name
return ""
def get_template_context(self):
context = super().get_template_context()
if self.upload_button:
context["upload_button"] = self.upload_button.get_template_context()
return context
def get_block_name(self):
return "textboxwithupload"
gr.components.COMPONENT_MAPPING["textboxwithupload"] = TextboxWithUpload