Spaces:
Build error
Build error
| 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) |