Spaces:
Running
Running
Jan Philip Wahle
commited on
Commit
β’
22f7204
1
Parent(s):
6b4ea40
Add demo
Browse files- README.md +70 -13
- app.py +72 -0
- requirements.txt +2 -0
README.md
CHANGED
@@ -1,13 +1,70 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Paraphrase Generator with OpenAI and Gradio
|
2 |
+
|
3 |
+
This is a simple demo of a paraphrase generator using OpenAI's GPT-3 model and Gradio for the user interface. The application allows the user to input a sentence and select a type of paraphrase. The application then generates a paraphrase of the input sentence based on the selected type.
|
4 |
+
|
5 |
+
## Getting Started
|
6 |
+
|
7 |
+
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
|
8 |
+
|
9 |
+
### Prerequisites
|
10 |
+
|
11 |
+
You need Python 3.7 or later to run the application. You can have multiple Python versions (2.x and 3.x) installed on the same system without problems.
|
12 |
+
|
13 |
+
In Ubuntu, Mint and Debian you can install Python 3 like this:
|
14 |
+
|
15 |
+
```
|
16 |
+
$ sudo apt-get install python3 python3-pip
|
17 |
+
```
|
18 |
+
|
19 |
+
For other Linux flavors, macOS and Windows, packages are available at
|
20 |
+
|
21 |
+
https://www.python.org/getit/
|
22 |
+
|
23 |
+
### Quick Start
|
24 |
+
|
25 |
+
Clone the repository:
|
26 |
+
|
27 |
+
```
|
28 |
+
$ git clone https://github.com/<your-github-username>/paraphrase-generator.git
|
29 |
+
$ cd paraphrase-generator
|
30 |
+
```
|
31 |
+
|
32 |
+
Install the requirements:
|
33 |
+
|
34 |
+
```
|
35 |
+
$ pip install -r requirements.txt
|
36 |
+
```
|
37 |
+
|
38 |
+
Run the application:
|
39 |
+
|
40 |
+
```
|
41 |
+
$ python app.py
|
42 |
+
```
|
43 |
+
|
44 |
+
## How to Use
|
45 |
+
|
46 |
+
1. Enter a sentence in the text box.
|
47 |
+
2. Select a type of paraphrase from the dropdown menu.
|
48 |
+
3. Click the "Submit" button.
|
49 |
+
4. The application will generate a paraphrase of the input sentence based on the selected type.
|
50 |
+
|
51 |
+
## Built With
|
52 |
+
|
53 |
+
* [Python](https://www.python.org/) - The programming language used.
|
54 |
+
* [Gradio](https://www.gradio.app/) - For creating the user interface.
|
55 |
+
* [OpenAI](https://www.openai.com/) - Used to generate the paraphrases.
|
56 |
+
|
57 |
+
## Authors
|
58 |
+
|
59 |
+
* **Jan Philip Wahle** - *Initial work* - [Your Github Username](https://github.com/<your-github-username>)
|
60 |
+
|
61 |
+
## License
|
62 |
+
|
63 |
+
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
|
64 |
+
|
65 |
+
## Acknowledgments
|
66 |
+
|
67 |
+
* Hat tip to anyone whose code was used
|
68 |
+
* Inspiration
|
69 |
+
* etc
|
70 |
+
```
|
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2023 by Jan Philip Wahle, https://jpwahle.com/
|
2 |
+
# All rights reserved.
|
3 |
+
|
4 |
+
import os
|
5 |
+
import random
|
6 |
+
import time
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
import openai
|
10 |
+
|
11 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
12 |
+
|
13 |
+
def create_prompt(sentence, paraphrase_type):
|
14 |
+
prompt = {
|
15 |
+
"messages": [
|
16 |
+
{
|
17 |
+
"role": "user",
|
18 |
+
"content": f"Given the following sentence, generate a paraphrase with the following types. Sentence: {sentence}. Paraphrase Types: {paraphrase_type}"
|
19 |
+
}
|
20 |
+
]
|
21 |
+
}
|
22 |
+
return prompt
|
23 |
+
|
24 |
+
with gr.Blocks() as demo:
|
25 |
+
description = gr.Markdown("""
|
26 |
+
## Paraphrase Type Generator
|
27 |
+
This demo uses a fine-tuned ChatGPT-3.5 model to generate paraphrases given specific paraphrase types.
|
28 |
+
|
29 |
+
**How to use:**
|
30 |
+
1. Select one or many type of paraphrase from the dropdown menu.
|
31 |
+
2. Enter a sentence in the text box.
|
32 |
+
3. Click the "Submit" button or hit enter.
|
33 |
+
4. The application will generate a paraphrase of the input sentence based on the selected type.
|
34 |
+
""")
|
35 |
+
chatbot = gr.Chatbot()
|
36 |
+
types = gr.Dropdown(
|
37 |
+
["Morphology-based changes", "Lexico-syntactic based changes", "Lexicon-based changes", "Syntax-based changes", "Discourse-based changes"],
|
38 |
+
value="Syntax-based changes",
|
39 |
+
multiselect=True,
|
40 |
+
allow_custom_value=True,
|
41 |
+
)
|
42 |
+
msg = gr.Textbox()
|
43 |
+
submit = gr.Button("Submit")
|
44 |
+
clear = gr.Button("Clear")
|
45 |
+
|
46 |
+
|
47 |
+
def user(user_message, history):
|
48 |
+
return "", history + [[user_message, None]]
|
49 |
+
|
50 |
+
def generate_paraphrase(user_message, paraphrase_type, history):
|
51 |
+
history[-1][1] = ""
|
52 |
+
prompt = create_prompt(history[-1][0], paraphrase_type)
|
53 |
+
bot_message = openai.ChatCompletion.create(
|
54 |
+
model="ft:gpt-3.5-turbo-0613:personal::7xbU0xQ2",
|
55 |
+
messages=prompt["messages"],
|
56 |
+
)
|
57 |
+
history[-1][1] = ""
|
58 |
+
for character in bot_message.choices[0].message.content:
|
59 |
+
history[-1][1] += character
|
60 |
+
time.sleep(0.01)
|
61 |
+
yield history
|
62 |
+
|
63 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
64 |
+
generate_paraphrase, [msg, types, chatbot], chatbot
|
65 |
+
)
|
66 |
+
submit.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
67 |
+
generate_paraphrase, [msg, types, chatbot], chatbot
|
68 |
+
)
|
69 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
70 |
+
|
71 |
+
demo.queue()
|
72 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
openai
|