Spaces:
Running
Running
initial commit
Browse files
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from fine_tune_hf import FinetuneHFModel
|
| 4 |
+
|
| 5 |
+
fine_tune_model = FinetuneHFModel()
|
| 6 |
+
|
| 7 |
+
playground = gr.Blocks()
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def review_training_choices(choice):
|
| 11 |
+
print(choice)
|
| 12 |
+
if choice == "Use Pipeline":
|
| 13 |
+
return gr.Row(visible=True)
|
| 14 |
+
else:
|
| 15 |
+
return gr.Row(visible=False)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def show_optional_fields(task):
|
| 19 |
+
if task == "question-answering":
|
| 20 |
+
return gr.TextArea(visible=True)
|
| 21 |
+
return gr.TextArea(visible=False)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def test_pipeline(task, model=None, prompt=None, context=None):
|
| 25 |
+
if model:
|
| 26 |
+
test = pipeline(task, model=model)
|
| 27 |
+
else:
|
| 28 |
+
if task == "ner":
|
| 29 |
+
test = pipeline(task, grouped_entities=True)
|
| 30 |
+
else:
|
| 31 |
+
test = pipeline(task)
|
| 32 |
+
if task == "question-answering":
|
| 33 |
+
if not context:
|
| 34 |
+
return "Context is required"
|
| 35 |
+
else:
|
| 36 |
+
result = test(question=prompt, context=context)
|
| 37 |
+
else:
|
| 38 |
+
result = test(prompt)
|
| 39 |
+
match task:
|
| 40 |
+
case "text-generation":
|
| 41 |
+
return gr.TextArea(result[0]["generated_text"])
|
| 42 |
+
case "fill-mask":
|
| 43 |
+
return gr.TextArea(result[0]["sequence"])
|
| 44 |
+
case "summarization":
|
| 45 |
+
return gr.TextArea(result[0]["summary_text"])
|
| 46 |
+
case "ner":
|
| 47 |
+
ner_result = "\n".join(
|
| 48 |
+
f"{k}={v}" for item in result for k, v in item.items() if k not in ["start", "end", "index"])
|
| 49 |
+
return gr.TextArea(ner_result.rstrip("\n"))
|
| 50 |
+
|
| 51 |
+
case "question-answering":
|
| 52 |
+
return gr.TextArea(result)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
with playground:
|
| 56 |
+
gr.Markdown("""
|
| 57 |
+
Try your ideas here. Select from Text, Image or Audio
|
| 58 |
+
""")
|
| 59 |
+
with gr.Tabs():
|
| 60 |
+
with gr.TabItem("Text"):
|
| 61 |
+
with gr.Row():
|
| 62 |
+
with gr.Column(scale=4):
|
| 63 |
+
radio = gr.Radio(
|
| 64 |
+
["Use Pipeline", "Fine Tune"],
|
| 65 |
+
label="Select Use Pipeline to try out HF models or Fine Tune to test it on your own datasets",
|
| 66 |
+
value="Use Pipeline",
|
| 67 |
+
interactive=True,
|
| 68 |
+
)
|
| 69 |
+
with gr.Column(scale=1):
|
| 70 |
+
test_pipeline_button = gr.Button(
|
| 71 |
+
value="Test", variant="primary", size="sm")
|
| 72 |
+
with gr.Row(visible=True) as use_pipeline:
|
| 73 |
+
with gr.Column():
|
| 74 |
+
task_dropdown = gr.Dropdown(
|
| 75 |
+
[("Text Generation", "text-generation"), ("Fill Mask",
|
| 76 |
+
"fill-mask"), ("Summarization", "summarization"), ("Named Entity Recognition", "ner"), ("Question Answering", "question-answering")],
|
| 77 |
+
label="task",
|
| 78 |
+
)
|
| 79 |
+
model_dropdown = gr.Dropdown(
|
| 80 |
+
[],
|
| 81 |
+
label="model",
|
| 82 |
+
allow_custom_value=True,
|
| 83 |
+
interactive=True
|
| 84 |
+
)
|
| 85 |
+
prompt_textarea = gr.TextArea(
|
| 86 |
+
label="prompt", value="Enter your prompt here", text_align="left")
|
| 87 |
+
context_for_question_answer = gr.TextArea(
|
| 88 |
+
label="Context", value="Enter Context for your question here", visible=False, interactive=True)
|
| 89 |
+
task_dropdown.change(show_optional_fields, inputs=[
|
| 90 |
+
task_dropdown], outputs=[context_for_question_answer])
|
| 91 |
+
with gr.Column():
|
| 92 |
+
text = gr.TextArea(label="Generated Text")
|
| 93 |
+
radio.change(review_training_choices,
|
| 94 |
+
inputs=radio, outputs=use_pipeline)
|
| 95 |
+
test_pipeline_button.click(test_pipeline, inputs=[
|
| 96 |
+
task_dropdown, model_dropdown, prompt_textarea, context_for_question_answer], outputs=text)
|
| 97 |
+
with gr.TabItem("Image"):
|
| 98 |
+
with gr.Row():
|
| 99 |
+
with gr.Column(scale=3):
|
| 100 |
+
radio = gr.Radio(
|
| 101 |
+
["Use Pipeline", "Fine Tune"],
|
| 102 |
+
label="Select Use Pipeline to try out HF models or Fine Tune to test it on your own datasets",
|
| 103 |
+
value="Use Pipeline",
|
| 104 |
+
interactive=True
|
| 105 |
+
)
|
| 106 |
+
with gr.Column(scale=1):
|
| 107 |
+
test_pipeline_button = gr.Button(
|
| 108 |
+
value="Test", variant="primary", size="sm")
|
| 109 |
+
with gr.TabItem("Audio"):
|
| 110 |
+
with gr.Row():
|
| 111 |
+
with gr.Column(scale=3):
|
| 112 |
+
radio = gr.Radio(
|
| 113 |
+
["Use Pipeline", "Fine Tune"],
|
| 114 |
+
label="Select Use Pipeline to try out HF models or Fine Tune to test it on your own datasets",
|
| 115 |
+
value="Use Pipeline",
|
| 116 |
+
interactive=True
|
| 117 |
+
)
|
| 118 |
+
with gr.Column(scale=1):
|
| 119 |
+
test_pipeline_button = gr.Button(
|
| 120 |
+
value="Test", variant="primary", size="sm")
|
| 121 |
+
|
| 122 |
+
playground.launch(share=True)
|