Anupam251272 commited on
Commit
4fbac78
·
verified ·
1 Parent(s): 62c9655

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -3,9 +3,7 @@ import logging
3
  import os
4
  import re
5
 
6
-
7
  import spaces
8
-
9
  import torch
10
  from cleantext import clean
11
  import gradio as gr
@@ -16,21 +14,25 @@ from transformers import AutoModelForSequenceClassification, AutoTokenizer
16
  logging.basicConfig(level=logging.INFO)
17
  logging.info(f"torch version:\t{torch.__version__}")
18
 
 
 
 
 
19
  # Model names
20
  checker_model_name = "textattack/roberta-base-CoLA"
21
  corrector_model_name = "pszemraj/flan-t5-large-grammar-synthesis"
22
 
23
-
24
  checker = pipeline(
25
  "text-classification",
26
  checker_model_name,
27
- device_map="cuda",
28
  )
29
 
30
  corrector = pipeline(
31
  "text2text-generation",
32
  corrector_model_name,
33
- device_map="cuda",
34
  )
35
 
36
  def split_text(text: str) -> list:
@@ -53,7 +55,6 @@ def split_text(text: str) -> list:
53
 
54
  @spaces.GPU(duration=60)
55
  def correct_text(text: str, separator: str = " ") -> str:
56
-
57
  # Split the text into sentence batches
58
  sentence_batches = split_text(text)
59
 
@@ -102,11 +103,11 @@ with gr.Blocks() as demo:
102
  )
103
  with gr.Row():
104
  inp = gr.Textbox(
105
- label="input",
106
  placeholder="Enter text to check & correct",
107
  value="I wen to the store yesturday to bye some food. I needd milk, bread, and a few otter things. The store was really crowed and I had a hard time finding everyting I needed. I finaly made it to the check out line and payed for my stuff.",
108
  )
109
- out = gr.Textbox(label="output", interactive=False)
110
  btn = gr.Button("Process")
111
  btn.click(fn=update, inputs=inp, outputs=out)
112
  gr.Markdown("---")
@@ -115,4 +116,4 @@ with gr.Blocks() as demo:
115
  )
116
 
117
  # Launch the demo
118
- demo.launch(debug=True)
 
3
  import os
4
  import re
5
 
 
6
  import spaces
 
7
  import torch
8
  from cleantext import clean
9
  import gradio as gr
 
14
  logging.basicConfig(level=logging.INFO)
15
  logging.info(f"torch version:\t{torch.__version__}")
16
 
17
+ # Ensure compatibility with GPU or CPU
18
+ device = "cuda" if torch.cuda.is_available() else "cpu"
19
+ logging.info(f"Using device: {device}")
20
+
21
  # Model names
22
  checker_model_name = "textattack/roberta-base-CoLA"
23
  corrector_model_name = "pszemraj/flan-t5-large-grammar-synthesis"
24
 
25
+ # Initialize pipelines with device mapping
26
  checker = pipeline(
27
  "text-classification",
28
  checker_model_name,
29
+ device=0 if torch.cuda.is_available() else -1, # 0 for GPU, -1 for CPU
30
  )
31
 
32
  corrector = pipeline(
33
  "text2text-generation",
34
  corrector_model_name,
35
+ device=0 if torch.cuda.is_available() else -1, # 0 for GPU, -1 for CPU
36
  )
37
 
38
  def split_text(text: str) -> list:
 
55
 
56
  @spaces.GPU(duration=60)
57
  def correct_text(text: str, separator: str = " ") -> str:
 
58
  # Split the text into sentence batches
59
  sentence_batches = split_text(text)
60
 
 
103
  )
104
  with gr.Row():
105
  inp = gr.Textbox(
106
+ label="Input",
107
  placeholder="Enter text to check & correct",
108
  value="I wen to the store yesturday to bye some food. I needd milk, bread, and a few otter things. The store was really crowed and I had a hard time finding everyting I needed. I finaly made it to the check out line and payed for my stuff.",
109
  )
110
+ out = gr.Textbox(label="Output", interactive=False)
111
  btn = gr.Button("Process")
112
  btn.click(fn=update, inputs=inp, outputs=out)
113
  gr.Markdown("---")
 
116
  )
117
 
118
  # Launch the demo
119
+ demo.launch(debug=True)