imseldrith commited on
Commit
7f15dc6
·
1 Parent(s): 73df650

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -22
app.py CHANGED
@@ -1,27 +1,55 @@
1
- from flask import Flask, request, render_template
2
- import requests
3
- import json
 
 
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- app = Flask(__name__)
 
 
 
 
 
 
 
 
 
 
7
 
8
- @app.route('/')
9
- def index():
10
- return render_template('index.html')
11
 
12
- @app.route('/paraphrase', methods=['POST'])
13
- def paraphrase():
14
- text = request.form['text']
15
- headers = {
16
- 'Content-Type': 'application/json',
17
- }
18
- data = {"text": text}
19
- response = requests.post('https://api.openai.com/v1/engines/paraphrase/jobs', headers=headers, json=data)
20
- if response.status_code == 200:
21
- response_text = response.json()['choices'][0]['text']
22
- return render_template('paraphrase.html', response_text=response_text)
23
- else:
24
- return 'An error occured'
25
 
26
- if __name__ == '__main__':
27
- app.run(host="0.0.0.0", port=7860)
 
1
+ import gradio as gr
2
+ from bs4 import BeautifulSoup
3
+ from nltk.tokenize import word_tokenize
4
+ from nltk.corpus import wordnet
5
+ import random
6
 
7
+ def paraphrase_text(text):
8
+ # Tokenize the text
9
+ tokens = word_tokenize(text)
10
+ # Create a list to hold the paraphrased words
11
+ paraphrased_tokens = []
12
+ for token in tokens:
13
+ # Check if the token is a word
14
+ if token.isalpha():
15
+ # Get the synonyms of the word
16
+ synonyms = []
17
+ for syn in wordnet.synsets(token):
18
+ for lemma in syn.lemmas():
19
+ if lemma.name() != token:
20
+ synonyms.append(lemma.name())
21
+ # If there are synonyms available, choose a random one
22
+ if synonyms:
23
+ paraphrased_word = random.choice(synonyms)
24
+ # If no synonyms are available, use the original word
25
+ else:
26
+ paraphrased_word = token
27
+ # If the token is not a word, use it as-is
28
+ else:
29
+ paraphrased_word = token
30
+ # Add the paraphrased word to the list
31
+ paraphrased_tokens.append(paraphrased_word)
32
+ # Join the paraphrased tokens back into a string
33
+ paraphrased_text = ' '.join(paraphrased_tokens)
34
+ return paraphrased_text
35
 
36
+ def paraphrase_html(html_text):
37
+ # Parse the HTML using BeautifulSoup
38
+ soup = BeautifulSoup(html_text, 'html.parser')
39
+ # Find all the text nodes in the HTML
40
+ text_nodes = soup.find_all(text=True)
41
+ # Paraphrase the text nodes
42
+ for node in text_nodes:
43
+ node.replace_with(paraphrase_text(node.string))
44
+ # Return the paraphrased HTML
45
+ paraphrased_html = str(soup)
46
+ return paraphrased_html
47
 
48
+ inputs = gr.inputs.Textbox(label="Enter HTML text to paraphrase")
49
+ outputs = gr.outputs.HTML(label="Paraphrased HTML")
 
50
 
51
+ title = "HTML Paraphraser"
52
+ description = "Enter HTML text and get a paraphrased version in HTML format."
53
+ examples = [["<p>This is some <b>HTML</b> text to paraphrase.</p>"]]
 
 
 
 
 
 
 
 
 
 
54
 
55
+ gr.Interface(paraphrase_html, inputs, outputs, title=title, description=description, examples=examples).launch()