Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import re
|
4 |
+
|
5 |
+
# Authenticate with OpenAI API
|
6 |
+
openai.api_key = "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"
|
7 |
+
|
8 |
+
# Set the model and prompt for GPT-2
|
9 |
+
model_engine = "text-davinci-002"
|
10 |
+
model_prompt = "Rewrite this text:"
|
11 |
+
|
12 |
+
# Define the function to generate the paraphrased output
|
13 |
+
def paraphrase(text):
|
14 |
+
# Format the input text for the model prompt
|
15 |
+
formatted_text = f"{model_prompt} {text}"
|
16 |
+
|
17 |
+
# Generate the paraphrased output
|
18 |
+
response = openai.Completion.create(
|
19 |
+
engine=model_engine,
|
20 |
+
prompt=formatted_text,
|
21 |
+
max_tokens=1024,
|
22 |
+
n=1,
|
23 |
+
stop=None,
|
24 |
+
temperature=0.7,
|
25 |
+
)
|
26 |
+
|
27 |
+
# Extract the paraphrased text from the response
|
28 |
+
paraphrased_text = response.choices[0].text
|
29 |
+
|
30 |
+
# Clean up the paraphrased text
|
31 |
+
paraphrased_text = re.sub(model_prompt, "", paraphrased_text)
|
32 |
+
paraphrased_text = paraphrased_text.strip()
|
33 |
+
|
34 |
+
return paraphrased_text
|
35 |
+
|
36 |
+
# Create the user interface
|
37 |
+
input_text = gr.inputs.Textbox(lines=20, label='Input Text')
|
38 |
+
output_text = gr.outputs.Textbox(label='Output Text')
|
39 |
+
|
40 |
+
app = gr.Interface(paraphrase, inputs=input_text, outputs=output_text, title='GPT-2 Article Rewriter', description='Generate a paraphrased output while preserving the meaning of the input text using OpenAI GPT-2 model.')
|
41 |
+
|
42 |
+
# Launch the user interface
|
43 |
+
app.launch()
|