Spaces:
Sleeping
Sleeping
Create textbox_with_upload.py
Browse files- textbox_with_upload.py +28 -0
textbox_with_upload.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gradio.components import Textbox
|
3 |
+
from gradio.components.base import Component
|
4 |
+
|
5 |
+
class TextboxWithUpload(Textbox):
|
6 |
+
def __init__(self, *args, **kwargs):
|
7 |
+
super().__init__(*args, **kwargs)
|
8 |
+
self.upload_button = None
|
9 |
+
|
10 |
+
def add_upload_button(self):
|
11 |
+
self.upload_button = gr.UploadButton("📎", file_types=["text"], file_count="single")
|
12 |
+
self.upload_button.upload(self.handle_upload, inputs=[self.upload_button], outputs=[self])
|
13 |
+
|
14 |
+
def handle_upload(self, file):
|
15 |
+
if file:
|
16 |
+
return file.name
|
17 |
+
return ""
|
18 |
+
|
19 |
+
def get_template_context(self):
|
20 |
+
context = super().get_template_context()
|
21 |
+
if self.upload_button:
|
22 |
+
context["upload_button"] = self.upload_button.get_template_context()
|
23 |
+
return context
|
24 |
+
|
25 |
+
def get_block_name(self):
|
26 |
+
return "textboxwithupload"
|
27 |
+
|
28 |
+
gr.components.COMPONENT_MAPPING["textboxwithupload"] = TextboxWithUpload
|