Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,96 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
-
|
4 |
import spaces
|
5 |
import torch
|
6 |
|
7 |
zero = torch.Tensor([0]).cuda()
|
8 |
print(zero.device)
|
9 |
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
@spaces.GPU
|
14 |
def generate_code(prompt):
|
15 |
-
output =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
return output[0]['generated_text']
|
17 |
|
|
|
|
|
|
|
|
|
|
|
18 |
# Gradio Interface
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
)
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
|
|
3 |
import spaces
|
4 |
import torch
|
5 |
|
6 |
zero = torch.Tensor([0]).cuda()
|
7 |
print(zero.device)
|
8 |
|
9 |
+
|
10 |
+
# Check if GPU is available for FP16 inference
|
11 |
+
device = 0 if torch.cuda.is_available() else -1
|
12 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
13 |
+
|
14 |
+
# Load Pipelines with FP16 (if GPU available)
|
15 |
+
question_answering = pipeline("question-answering", model="deepset/roberta-base-squad2", device=device)
|
16 |
+
code_generation = pipeline("text-generation", model="Salesforce/codegen-350M-mono", device=device)
|
17 |
+
summarization = pipeline("summarization", model="facebook/bart-large-cnn", device=device)
|
18 |
+
translation = pipeline("translation_en_to_fr", model="Helsinki-NLP/opus-mt-en-fr", device=device)
|
19 |
+
text_generation = pipeline("text-generation", model="gpt2", device=device)
|
20 |
+
text_classification = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english", device=device)
|
21 |
+
|
22 |
+
# Define Functions for Each Task
|
23 |
+
@spaces.GPU
|
24 |
+
def answer_question(context, question):
|
25 |
+
result = question_answering(question=question, context=context)
|
26 |
+
return result["answer"]
|
27 |
|
28 |
@spaces.GPU
|
29 |
def generate_code(prompt):
|
30 |
+
output = code_generation(prompt, max_length=50)
|
31 |
+
return output[0]['generated_text']
|
32 |
+
|
33 |
+
@spaces.GPU
|
34 |
+
def summarize_text(text):
|
35 |
+
output = summarization(text, max_length=100, min_length=30, do_sample=False)
|
36 |
+
return output[0]['summary_text']
|
37 |
+
|
38 |
+
@spaces.GPU
|
39 |
+
def translate_text(text):
|
40 |
+
output = translation(text)
|
41 |
+
return output[0]['translation_text']
|
42 |
+
|
43 |
+
@spaces.GPU
|
44 |
+
def generate_text(prompt):
|
45 |
+
output = text_generation(prompt, max_length=100)
|
46 |
return output[0]['generated_text']
|
47 |
|
48 |
+
@spaces.GPU
|
49 |
+
def classify_text(text):
|
50 |
+
output = text_classification(text)
|
51 |
+
return f"Label: {output[0]['label']} | Score: {output[0]['score']:.4f}"
|
52 |
+
|
53 |
# Gradio Interface
|
54 |
+
with gr.Blocks() as demo:
|
55 |
+
gr.Markdown("# 🤖 Transformers Pipeline with FP16 Inference")
|
56 |
+
|
57 |
+
with gr.Tab("1️⃣ Question Answering"):
|
58 |
+
with gr.Row():
|
59 |
+
context = gr.Textbox(label="Context", lines=4, placeholder="Paste your paragraph here...")
|
60 |
+
question = gr.Textbox(label="Question", placeholder="Ask a question...")
|
61 |
+
answer_btn = gr.Button("Get Answer")
|
62 |
+
answer_output = gr.Textbox(label="Answer")
|
63 |
+
answer_btn.click(answer_question, inputs=[context, question], outputs=answer_output)
|
64 |
+
|
65 |
+
with gr.Tab("2️⃣ Code Generation"):
|
66 |
+
code_input = gr.Textbox(label="Code Prompt", placeholder="Write code snippet...")
|
67 |
+
code_btn = gr.Button("Generate Code")
|
68 |
+
code_output = gr.Textbox(label="Generated Code")
|
69 |
+
code_btn.click(generate_code, inputs=code_input, outputs=code_output)
|
70 |
+
|
71 |
+
with gr.Tab("3️⃣ Summarization"):
|
72 |
+
summary_input = gr.Textbox(label="Text to Summarize", lines=5, placeholder="Paste long text here...")
|
73 |
+
summary_btn = gr.Button("Summarize")
|
74 |
+
summary_output = gr.Textbox(label="Summary")
|
75 |
+
summary_btn.click(summarize_text, inputs=summary_input, outputs=summary_output)
|
76 |
+
|
77 |
+
with gr.Tab("4️⃣ Translation (EN → FR)"):
|
78 |
+
translate_input = gr.Textbox(label="English Text", placeholder="Enter text in English...")
|
79 |
+
translate_btn = gr.Button("Translate")
|
80 |
+
translate_output = gr.Textbox(label="French Translation")
|
81 |
+
translate_btn.click(translate_text, inputs=translate_input, outputs=translate_output)
|
82 |
+
|
83 |
+
with gr.Tab("5️⃣ Text Generation"):
|
84 |
+
text_input = gr.Textbox(label="Text Prompt", placeholder="Start your text...")
|
85 |
+
text_btn = gr.Button("Generate Text")
|
86 |
+
text_output = gr.Textbox(label="Generated Text")
|
87 |
+
text_btn.click(generate_text, inputs=text_input, outputs=text_output)
|
88 |
+
|
89 |
+
with gr.Tab("6️⃣ Text Classification"):
|
90 |
+
classify_input = gr.Textbox(label="Enter Text", placeholder="Enter a sentence...")
|
91 |
+
classify_btn = gr.Button("Classify Sentiment")
|
92 |
+
classify_output = gr.Textbox(label="Classification Result")
|
93 |
+
classify_btn.click(classify_text, inputs=classify_input, outputs=classify_output)
|
94 |
+
|
95 |
+
# Launch App
|
96 |
+
demo.launch()
|