Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
def model_inference(model_name, task, input_data):
|
5 |
+
try:
|
6 |
+
# Load the model pipeline dynamically based on user selection
|
7 |
+
model_pipeline = pipeline(task, model=model_name)
|
8 |
+
# Perform the inference
|
9 |
+
result = model_pipeline(input_data, max_length=100)
|
10 |
+
# Handle different output formats
|
11 |
+
if isinstance(result, list):
|
12 |
+
return result[0]['generated_text'] if 'generated_text' in result[0] else str(result)
|
13 |
+
return result
|
14 |
+
except Exception as e:
|
15 |
+
# Return error message to the user interface
|
16 |
+
return f"An error occurred: {str(e)}"
|
17 |
+
|
18 |
+
def setup_interface():
|
19 |
+
# Define the available models and tasks
|
20 |
+
models = {
|
21 |
+
"Text Generation": ["gpt2", "EleutherAI/gpt-neo-2.7B"],
|
22 |
+
"Text Classification": ["bert-base-uncased", "roberta-base"],
|
23 |
+
"Token Classification": ["dbmdz/bert-large-cased-finetuned-conll03-english"]
|
24 |
+
}
|
25 |
+
|
26 |
+
tasks = {
|
27 |
+
"Text Generation": "text-generation",
|
28 |
+
"Text Classification": "text-classification",
|
29 |
+
"Token Classification": "token-classification"
|
30 |
+
}
|
31 |
+
|
32 |
+
with gr.Blocks() as demo:
|
33 |
+
gr.Markdown("### Hugging Face Model Playground")
|
34 |
+
with gr.Row():
|
35 |
+
selected_task = gr.Dropdown(label="Select Task", choices=list(models.keys()), value="Text Generation")
|
36 |
+
model_name = gr.Dropdown(label="Select Model", choices=models[selected_task.value])
|
37 |
+
input_data = gr.Textbox(label="Input", placeholder="Type here...")
|
38 |
+
output = gr.Textbox(label="Output", placeholder="Results will appear here...")
|
39 |
+
|
40 |
+
# Update the model dropdown based on task selection
|
41 |
+
def update_models(task):
|
42 |
+
return gr.Dropdown.update(choices=models[task])
|
43 |
+
|
44 |
+
selected_task.change(fn=update_models, inputs=selected_task, outputs=model_name)
|
45 |
+
|
46 |
+
# Run model inference when input data changes
|
47 |
+
input_data.change(fn=model_inference, inputs=[model_name, selected_task, input_data], outputs=output)
|
48 |
+
|
49 |
+
return demo
|
50 |
+
|
51 |
+
if __name__ == "__main__":
|
52 |
+
interface = setup_interface()
|
53 |
+
interface.launch()
|