Alexvatti commited on
Commit
493c901
·
verified ·
1 Parent(s): 16e9ab4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -51
app.py CHANGED
@@ -12,24 +12,8 @@ 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
- #translation = pipeline("translation", model="facebook/m2m100_418M", device=device)
20
- text_generation = pipeline("text-generation", model="gpt2", device=device)
21
- text_classification = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english", device=device)
22
 
23
- # Define Functions for Each Task
24
- @spaces.GPU
25
- def answer_question(context, question):
26
- result = question_answering(question=question, context=context)
27
- return result["answer"]
28
-
29
- @spaces.GPU
30
- def generate_code(prompt):
31
- output = code_generation(prompt, max_length=50)
32
- return output[0]['generated_text']
33
 
34
  @spaces.GPU
35
  def summarize_text(text):
@@ -38,51 +22,19 @@ def summarize_text(text):
38
 
39
 
40
 
41
- @spaces.GPU
42
- def generate_text(prompt):
43
- output = text_generation(prompt, max_length=100)
44
- return output[0]['generated_text']
45
-
46
- @spaces.GPU
47
- def classify_text(text):
48
- output = text_classification(text)
49
- return f"Label: {output[0]['label']} | Score: {output[0]['score']:.4f}"
50
 
51
  # Gradio Interface
52
  with gr.Blocks() as demo:
53
  gr.Markdown("# 🤖 Transformers Pipeline with FP16 Inference")
54
 
55
- with gr.Tab("1️⃣ Question Answering"):
56
- with gr.Row():
57
- context = gr.Textbox(label="Context", lines=4, placeholder="Paste your paragraph here...")
58
- question = gr.Textbox(label="Question", placeholder="Ask a question...")
59
- answer_btn = gr.Button("Get Answer")
60
- answer_output = gr.Textbox(label="Answer")
61
- answer_btn.click(answer_question, inputs=[context, question], outputs=answer_output)
62
 
63
- with gr.Tab("2️⃣ Code Generation"):
64
- code_input = gr.Textbox(label="Code Prompt", placeholder="Write code snippet...")
65
- code_btn = gr.Button("Generate Code")
66
- code_output = gr.Textbox(label="Generated Code")
67
- code_btn.click(generate_code, inputs=code_input, outputs=code_output)
68
-
69
- with gr.Tab("3️⃣ Summarization"):
70
- summary_input = gr.Textbox(label="Text to Summarize", lines=5, placeholder="Paste long text here...")
71
  summary_btn = gr.Button("Summarize")
72
  summary_output = gr.Textbox(label="Summary")
73
  summary_btn.click(summarize_text, inputs=summary_input, outputs=summary_output)
74
 
75
- with gr.Tab("4️⃣ Text Generation"):
76
- text_input = gr.Textbox(label="Text Prompt", placeholder="Start your text...")
77
- text_btn = gr.Button("Generate Text")
78
- text_output = gr.Textbox(label="Generated Text")
79
- text_btn.click(generate_text, inputs=text_input, outputs=text_output)
80
-
81
- with gr.Tab("5️⃣ Text Classification"):
82
- classify_input = gr.Textbox(label="Enter Text", placeholder="Enter a sentence...")
83
- classify_btn = gr.Button("Classify Sentiment")
84
- classify_output = gr.Textbox(label="Classification Result")
85
- classify_btn.click(classify_text, inputs=classify_input, outputs=classify_output)
86
 
87
  # Launch App
88
  demo.launch()
 
12
  torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
13
 
14
  # Load Pipelines with FP16 (if GPU available)
 
 
15
  summarization = pipeline("summarization", model="facebook/bart-large-cnn", device=device)
 
 
 
 
16
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  @spaces.GPU
19
  def summarize_text(text):
 
22
 
23
 
24
 
 
 
 
 
 
 
 
 
 
25
 
26
  # Gradio Interface
27
  with gr.Blocks() as demo:
28
  gr.Markdown("# 🤖 Transformers Pipeline with FP16 Inference")
29
 
 
 
 
 
 
 
 
30
 
31
+ with gr.Tab("Text Summarization"):
32
+ summary_input = gr.Textbox(label="Text to Summarize ", lines=5, placeholder="Paste long text here...")
 
 
 
 
 
 
33
  summary_btn = gr.Button("Summarize")
34
  summary_output = gr.Textbox(label="Summary")
35
  summary_btn.click(summarize_text, inputs=summary_input, outputs=summary_output)
36
 
37
+
 
 
 
 
 
 
 
 
 
 
38
 
39
  # Launch App
40
  demo.launch()