nsethi610 commited on
Commit
992d89d
1 Parent(s): b8dad8b

refactored app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -99
app.py CHANGED
@@ -1,119 +1,60 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
 
3
 
4
  playground = gr.Blocks()
5
 
6
-
7
- def review_training_choices(choice):
8
- print(choice)
9
- if choice == "Use Pipeline":
10
- return gr.Row(visible=True)
11
- else:
12
- return gr.Row(visible=False)
13
-
14
-
15
- def show_optional_fields(task):
16
- if task == "question-answering":
17
- return gr.TextArea(visible=True)
18
- return gr.TextArea(visible=False)
19
-
20
-
21
- def test_pipeline(task, model=None, prompt=None, context=None):
22
- if model:
23
- test = pipeline(task, model=model)
24
- else:
25
- if task == "ner":
26
- test = pipeline(task, grouped_entities=True)
27
- else:
28
- test = pipeline(task)
29
- if task == "question-answering":
30
- if not context:
31
- return "Context is required"
32
- else:
33
- result = test(question=prompt, context=context)
34
- else:
35
- result = test(prompt)
36
- match task:
37
- case "text-generation":
38
- return gr.TextArea(result[0]["generated_text"])
39
- case "fill-mask":
40
- return gr.TextArea(result[0]["sequence"])
41
- case "summarization":
42
- return gr.TextArea(result[0]["summary_text"])
43
- case "ner":
44
- ner_result = "\n".join(
45
- f"{k}={v}" for item in result for k, v in item.items() if k not in ["start", "end", "index"])
46
- return gr.TextArea(ner_result.rstrip("\n"))
47
-
48
- case "question-answering":
49
- return gr.TextArea(result)
50
-
51
-
52
  with playground:
53
- gr.Markdown("""
54
- Try your ideas here. Select from Text, Image or Audio
55
- """)
56
  with gr.Tabs():
57
  with gr.TabItem("Text"):
58
- with gr.Row():
59
- with gr.Column(scale=4):
60
- radio = gr.Radio(
61
- ["Use Pipeline", "Fine Tune"],
62
- label="Select Use Pipeline to try out HF models or Fine Tune to test it on your own datasets",
63
- value="Use Pipeline",
64
- interactive=True,
65
- )
66
- with gr.Column(scale=1):
67
- test_pipeline_button = gr.Button(
68
- value="Test", variant="primary", size="sm")
69
  with gr.Row(visible=True) as use_pipeline:
70
  with gr.Column():
71
  task_dropdown = gr.Dropdown(
72
- [("Text Generation", "text-generation"), ("Fill Mask",
73
- "fill-mask"), ("Summarization", "summarization"), ("Named Entity Recognition", "ner"), ("Question Answering", "question-answering")],
74
- label="task",
 
 
75
  )
76
  model_dropdown = gr.Dropdown(
77
- [],
78
- label="model",
79
- allow_custom_value=True,
80
- interactive=True
81
- )
82
  prompt_textarea = gr.TextArea(
83
- label="prompt", value="Enter your prompt here", text_align="left")
 
 
 
 
84
  context_for_question_answer = gr.TextArea(
85
- label="Context", value="Enter Context for your question here", visible=False, interactive=True)
86
- task_dropdown.change(show_optional_fields, inputs=[
87
- task_dropdown], outputs=[context_for_question_answer])
 
 
 
 
 
 
 
88
  with gr.Column():
89
  text = gr.TextArea(label="Generated Text")
90
  radio.change(review_training_choices,
91
  inputs=radio, outputs=use_pipeline)
92
- test_pipeline_button.click(test_pipeline, inputs=[
93
- task_dropdown, model_dropdown, prompt_textarea, context_for_question_answer], outputs=text)
 
 
94
  with gr.TabItem("Image"):
95
- with gr.Row():
96
- with gr.Column(scale=3):
97
- radio = gr.Radio(
98
- ["Use Pipeline", "Fine Tune"],
99
- label="Select Use Pipeline to try out HF models or Fine Tune to test it on your own datasets",
100
- value="Use Pipeline",
101
- interactive=True
102
- )
103
- with gr.Column(scale=1):
104
- test_pipeline_button = gr.Button(
105
- value="Test", variant="primary", size="sm")
106
  with gr.TabItem("Audio"):
107
- with gr.Row():
108
- with gr.Column(scale=3):
109
- radio = gr.Radio(
110
- ["Use Pipeline", "Fine Tune"],
111
- label="Select Use Pipeline to try out HF models or Fine Tune to test it on your own datasets",
112
- value="Use Pipeline",
113
- interactive=True
114
- )
115
- with gr.Column(scale=1):
116
- test_pipeline_button = gr.Button(
117
- value="Test", variant="primary", size="sm")
118
-
119
- playground.launch(share=True)
 
1
  import gradio as gr
2
+ from task import tasks_config
3
+ from pipeline_utils import handle_task_change, review_training_choices, test_pipeline
4
+ from playground_utils import create_playground_header, create_playground_footer, create_tabs_header
5
 
6
  playground = gr.Blocks()
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  with playground:
9
+ create_playground_header()
 
 
10
  with gr.Tabs():
11
  with gr.TabItem("Text"):
12
+ radio, test_pipeline_button = create_tabs_header()
 
 
 
 
 
 
 
 
 
 
13
  with gr.Row(visible=True) as use_pipeline:
14
  with gr.Column():
15
  task_dropdown = gr.Dropdown(
16
+ choices=[(task["name"], task_id)
17
+ for task_id, task in tasks_config.items()],
18
+ label="Task",
19
+ interactive=True,
20
+ info="Select Pipelines for natural language processing tasks or type if you have your own."
21
  )
22
  model_dropdown = gr.Dropdown(
23
+ [], label="Model", info="Select appropriate Model based on the task you selected")
 
 
 
 
24
  prompt_textarea = gr.TextArea(
25
+ label="Prompt",
26
+ value="Enter your prompt here",
27
+ text_align="left",
28
+ info="Copy/Paste or type your prompt to try out. Make sure to provide clear prompt or try with different prompts"
29
+ )
30
  context_for_question_answer = gr.TextArea(
31
+ label="Context",
32
+ value="Enter Context for your question here",
33
+ visible=False,
34
+ interactive=True,
35
+ info="Question answering tasks return an answer given a question. If you’ve ever asked a virtual assistant like Alexa, Siri or Google what the weather is, then you’ve used a question answering model before. Here, we are doing Extractive(extract the answer from the given context) Question answering. "
36
+ )
37
+ task_dropdown.change(handle_task_change,
38
+ inputs=[task_dropdown],
39
+ outputs=[context_for_question_answer,
40
+ model_dropdown, task_dropdown])
41
  with gr.Column():
42
  text = gr.TextArea(label="Generated Text")
43
  radio.change(review_training_choices,
44
  inputs=radio, outputs=use_pipeline)
45
+ test_pipeline_button.click(test_pipeline,
46
+ inputs=[
47
+ task_dropdown, model_dropdown, prompt_textarea, context_for_question_answer],
48
+ outputs=text)
49
  with gr.TabItem("Image"):
50
+ radio, test_pipeline_button = create_tabs_header()
51
+ gr.Markdown("""
52
+ > WIP
53
+ """)
 
 
 
 
 
 
 
54
  with gr.TabItem("Audio"):
55
+ radio, test_pipeline_button = create_tabs_header()
56
+ gr.Markdown("""
57
+ > WIP
58
+ """)
59
+ create_playground_footer()
60
+ playground.launch()