hotech commited on
Commit
53bac69
·
verified ·
1 Parent(s): ae71aec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -25
app.py CHANGED
@@ -1,13 +1,23 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- import os
4
 
5
- # Initialize model at startup
6
- model = pipeline(
7
- "text2text-generation",
8
- model="humarin/chatgpt_paraphraser_on_T5_base",
9
- device="cpu"
10
- )
 
 
 
 
 
 
 
 
 
 
11
 
12
  def paraphrase(text):
13
  try:
@@ -19,37 +29,33 @@ def paraphrase(text):
19
  )
20
  return output[0]["generated_text"]
21
  except Exception as e:
22
- return f"Error: {str(e)}"
 
23
 
24
- # Create interface with explicit API routes
25
  with gr.Blocks() as demo:
26
- gr.Markdown("## T5 Paraphraser API")
 
27
  with gr.Row():
28
- input_text = gr.Textbox(label="Input", lines=3)
29
- output_text = gr.Textbox(label="Output", lines=3)
30
 
31
- # Main function
32
- submit = gr.Button("Submit")
33
- submit.click(
34
  fn=paraphrase,
35
  inputs=input_text,
36
  outputs=output_text,
37
- api_name="paraphrase" # Critical: Names the API endpoint
38
  )
39
 
40
- # API examples
41
  gr.Examples(
42
  examples=["The quick brown fox jumps over the lazy dog."],
43
- inputs=input_text,
44
- outputs=output_text,
45
- fn=paraphrase,
46
- cache_examples=True
47
  )
48
 
49
- # Launch configuration
50
  demo.launch(
51
- show_api=True,
52
- enable_queue=True,
53
  server_name="0.0.0.0",
54
- server_port=7860
 
55
  )
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import time
4
 
5
+ # Show loading status
6
+ print("Loading model...")
7
+ start_time = time.time()
8
+
9
+ # Initialize model with error handling
10
+ try:
11
+ model = pipeline(
12
+ "text2text-generation",
13
+ model="humarin/chatgpt_paraphraser_on_T5_base",
14
+ device="cpu"
15
+ )
16
+ load_time = time.time() - start_time
17
+ print(f"Model loaded successfully in {load_time:.2f} seconds")
18
+ except Exception as e:
19
+ print(f"Model loading failed: {str(e)}")
20
+ raise
21
 
22
  def paraphrase(text):
23
  try:
 
29
  )
30
  return output[0]["generated_text"]
31
  except Exception as e:
32
+ print(f"Generation error: {str(e)}")
33
+ return text
34
 
35
+ # Create interface with modern Gradio API
36
  with gr.Blocks() as demo:
37
+ gr.Markdown("## T5 Paraphraser")
38
+
39
  with gr.Row():
40
+ input_text = gr.Textbox(label="Input Text", lines=3)
41
+ output_text = gr.Textbox(label="Paraphrased Text", lines=3)
42
 
43
+ submit_btn = gr.Button("Paraphrase")
44
+ submit_btn.click(
 
45
  fn=paraphrase,
46
  inputs=input_text,
47
  outputs=output_text,
48
+ api_name="predict" # Standard endpoint name
49
  )
50
 
 
51
  gr.Examples(
52
  examples=["The quick brown fox jumps over the lazy dog."],
53
+ inputs=input_text
 
 
 
54
  )
55
 
56
+ # Launch configuration for current Gradio
57
  demo.launch(
 
 
58
  server_name="0.0.0.0",
59
+ server_port=7860,
60
+ show_api=True
61
  )