choupijiang / textbox_with_upload.py
luminoussg's picture
Update textbox_with_upload.py
74f0713 verified
raw
history blame
943 Bytes
import gradio as gr
from gradio.components import Textbox
from gradio.components.base import Component
from gradio import component
@component
class TextboxWithUpload(Textbox):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.upload_button = None
self.add_upload_button()
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"