network01 commited on
Commit
75b10dc
·
verified ·
1 Parent(s): ee94055

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -8
app.py CHANGED
@@ -1,13 +1,44 @@
1
  import gradio as gr
 
2
 
3
- def greet(name):
4
- return f"Hello {name}, welcome to my first Space."
5
-
6
- demo = gr.Interface(
7
- fn=greet,
8
- inputs="text",
9
- outputs="text",
10
- title="My First Hugging Face Space"
11
  )
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
 
4
+ # 使用一个轻量文本生成模型(CPU 可跑)
5
+ generator = pipeline(
6
+ "text-generation",
7
+ model="distilgpt2"
 
 
 
 
8
  )
9
 
10
+ def rewrite(text, style):
11
+ if style == "More formal":
12
+ prompt = f"Rewrite this sentence in a more formal tone:\n{text}"
13
+ elif style == "More friendly":
14
+ prompt = f"Rewrite this sentence in a friendly tone:\n{text}"
15
+ else:
16
+ prompt = f"Rewrite this sentence to be shorter:\n{text}"
17
+
18
+ result = generator(prompt, max_length=100, num_return_sequences=1)
19
+ return result[0]["generated_text"]
20
+
21
+ with gr.Blocks(title="AI Text Rewriter") as demo:
22
+ gr.Markdown(
23
+ "### ✨ AI Text Rewriter\n"
24
+ "Paste a sentence and let AI rewrite it for you."
25
+ )
26
+
27
+ text_input = gr.Textbox(
28
+ label="Your text",
29
+ lines=3,
30
+ placeholder="Type something here..."
31
+ )
32
+
33
+ style = gr.Radio(
34
+ ["More formal", "More friendly", "Shorter"],
35
+ label="Rewrite style",
36
+ value="More friendly"
37
+ )
38
+
39
+ output = gr.Textbox(label="AI Result", lines=4)
40
+
41
+ btn = gr.Button("Rewrite with AI")
42
+ btn.click(fn=rewrite, inputs=[text_input, style], outputs=output)
43
+
44
  demo.launch()