File size: 1,748 Bytes
fd51240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72e6097
 
 
fd51240
 
4f5bdfd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import gradio as gr
import openai
import re

# Authenticate with OpenAI API
openai.api_key = "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"

# Set the model and prompt for GPT-2
model_engine = "text-davinci-002"
model_prompt = "Rewrite this text:"

# Define the function to generate the paraphrased output
def paraphrase(text):
    # Format the input text for the model prompt
    formatted_text = f"{model_prompt} {text}"

    # Generate the paraphrased output
    response = openai.Completion.create(
        engine=model_engine,
        prompt=formatted_text,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.7,
    )

    # Extract the paraphrased text from the response
    paraphrased_text = response.choices[0].text

    # Clean up the paraphrased text
    paraphrased_text = re.sub(model_prompt, "", paraphrased_text)
    paraphrased_text = paraphrased_text.strip()

    return paraphrased_text

# Create the user interface
input_text = gr.inputs.Textbox(lines=20, label='Input Text')
output_text = gr.outputs.Textbox(label='Output Text')

app = gr.Interface(paraphrase, inputs=input_text,css="""span.svelte-1l2rj76{color: #591fc9;font-size: 18px;
    font-weight: 600;}.secondary.svelte-1ma3u5b{background: #591fc9; color: #fff;}.secondary.svelte-1ma3u5b:hover{background:#8a59e8;color:#000;}
    .svelte-2xzfnp textarea {border: 1px solid #591fc9}.primary.svelte-1ma3u5b{background: #f8d605;color: #000;}.primary.svelte-1ma3u5b:hover{background: #ffe751;color: #591fc9;}  """, 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.')

# Launch the user interface
app.launch(inline=False)