Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
from IPython.display import Markdown, display
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
with open('mykey.txt', 'r') as file:
|
| 8 |
+
openai_key = file.read()
|
| 9 |
+
|
| 10 |
+
os.environ['OPENAI_API_KEY'] = openai_key
|
| 11 |
+
|
| 12 |
+
def get_completion_messages_gpt(messages, model="gpt-4o", temperature=0, presence_penalty=0, seed = 10, stream=False):
|
| 13 |
+
response = openai.chat.completions.create(
|
| 14 |
+
model=model,
|
| 15 |
+
messages=messages,
|
| 16 |
+
temperature=temperature,
|
| 17 |
+
presence_penalty=presence_penalty,
|
| 18 |
+
seed=seed,
|
| 19 |
+
stream = stream
|
| 20 |
+
)
|
| 21 |
+
return response.choices[0].message.content
|
| 22 |
+
|
| 23 |
+
system_message_simple = f"""
|
| 24 |
+
You are a Blog editor at Analytics Vidhya. Analytics Vidhya is an edtech platform focussing on creating
|
| 25 |
+
learning content for topics related to Data science, AI, Machine Learning and especially Generative AI.\
|
| 26 |
+
You crtically evaluate for the following parameters in the blogs you edit:\
|
| 27 |
+
|
| 28 |
+
1) Appropriate language and grammar structure
|
| 29 |
+
2) Correct word usage
|
| 30 |
+
3) Flow and struture of the article
|
| 31 |
+
4) Technical soundness of the article
|
| 32 |
+
5) Whether appropriate reference links are provided wherever required, especially where third party information is mentioned
|
| 33 |
+
6) FAQs are relevant to the article and non-repetetive
|
| 34 |
+
|
| 35 |
+
Rate the article on these parameters on a scale of 10 and provide suggestions to improve the respective parameter scores with \
|
| 36 |
+
specific suggestions.
|
| 37 |
+
For the parameters - use the keys mentioned in the json format below.
|
| 38 |
+
For the above parameters use sub-keys - Score and Suggestions, to show the respective output.
|
| 39 |
+
|
| 40 |
+
Also provide Top 5 suggestions for improvement in the article.
|
| 41 |
+
|
| 42 |
+
Finally provide the edited article incorporating all the suggestions. \
|
| 43 |
+
While editing the article also check whether the content of the article is human written or generated by generative AI tools like chatGPT.
|
| 44 |
+
Articles which have a high percentage of generative AI content rank lower on SEO, so your goal is to edit articles in a way\
|
| 45 |
+
that the artcle is more than 90% human written even after editing. Give the percentatge of content written by Generative AI\
|
| 46 |
+
before and after editing.
|
| 47 |
+
|
| 48 |
+
Compare the original article with the edited article and in the edited article italicise the changes made compared \
|
| 49 |
+
to the original article using markdown and mention the text which should be hyperlinked with a reference link within double exclamation marks.
|
| 50 |
+
|
| 51 |
+
Provide your output in JSON format with the keys:
|
| 52 |
+
Word count, Language & Grammar, Word Usage, Flow & Structure, Technical Soundness, Reference Links, FAQs, Top5 Suggestions, Edited Article, \
|
| 53 |
+
PreEditing GenAi Content%, PreEditing GenAi Content%.
|
| 54 |
+
Format the output such that the output can be directly used by python json library.
|
| 55 |
+
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
# Function to process the input article
|
| 59 |
+
def process_article(article):
|
| 60 |
+
messages = [
|
| 61 |
+
{'role': 'system', 'content': system_message_simple},
|
| 62 |
+
{'role': 'user', 'content': article}
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
response = get_completion_messages_gpt(messages)
|
| 66 |
+
clean_response = response.strip('```json').strip('```')
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
parsed_response = json.loads(clean_response)
|
| 70 |
+
except json.JSONDecodeError:
|
| 71 |
+
return ["Invalid response received from OpenAI"] * 10
|
| 72 |
+
|
| 73 |
+
# Extract different parts of the response
|
| 74 |
+
word_count = json.dumps(parsed_response.get('Word count', {}), indent=2)
|
| 75 |
+
language_grammar = json.dumps(parsed_response.get('Language & Grammar', {}), indent=2)
|
| 76 |
+
word_usage = json.dumps(parsed_response.get('Word Usage', {}), indent=2)
|
| 77 |
+
flow_structure = json.dumps(parsed_response.get('Flow & Structure', {}), indent=2)
|
| 78 |
+
technical_soundness = json.dumps(parsed_response.get('Technical Soundness', {}), indent=2)
|
| 79 |
+
reference_links = json.dumps(parsed_response.get('Reference Links', {}), indent=2)
|
| 80 |
+
faqs = json.dumps(parsed_response.get('FAQs', {}), indent=2)
|
| 81 |
+
top_suggestions = json.dumps(parsed_response.get('Top5 Suggestions', []), indent=2)
|
| 82 |
+
edited_article = parsed_response.get('Edited Article', "No edited article found")
|
| 83 |
+
pre_editing_genai = json.dumps(parsed_response.get('PreEditing GenAi Content%', {}), indent=2)
|
| 84 |
+
post_editing_genai = json.dumps(parsed_response.get('PostEditing GenAi Content%', {}), indent=2)
|
| 85 |
+
|
| 86 |
+
return (word_count, language_grammar, word_usage, flow_structure, technical_soundness,
|
| 87 |
+
reference_links, faqs, top_suggestions, edited_article, pre_editing_genai, post_editing_genai)
|
| 88 |
+
|
| 89 |
+
# Gradio Interface with improved layout
|
| 90 |
+
with gr.Blocks() as demo:
|
| 91 |
+
gr.Markdown("## AV Blog Editor V1")
|
| 92 |
+
gr.Markdown("### Guidelines (make sure to read these before using)")
|
| 93 |
+
gr.Markdown("""
|
| 94 |
+
1. Use your discretion while using this tool
|
| 95 |
+
2. The italicised content in the edited article represents the changes made by GPT. Make sure to review the changes.
|
| 96 |
+
3. The text under !! in the edited article shows approximate areas where reference link is required. The link it shows may not be correct.
|
| 97 |
+
4. The score and suggestions of various sections are good pointers to reflect back on your article.
|
| 98 |
+
5. The percentage of GPT generated content will not match what zerogpt.com says. Go with what zerogpt says.
|
| 99 |
+
6. Make sure to check zerogpt before using the edited article
|
| 100 |
+
7. Do share any suggestions for improvements in this tool with Apoorv. You can get some candies for that :)
|
| 101 |
+
""")
|
| 102 |
+
|
| 103 |
+
# Input article in a single row
|
| 104 |
+
with gr.Row():
|
| 105 |
+
article_input = gr.Textbox(label="Input Article", lines=15, placeholder="Enter your article here...")
|
| 106 |
+
|
| 107 |
+
# Outputs in two columns
|
| 108 |
+
with gr.Row():
|
| 109 |
+
with gr.Column():
|
| 110 |
+
word_count = gr.Textbox(label="Word Count")
|
| 111 |
+
language_grammar = gr.Textbox(label="Language & Grammar", lines=3)
|
| 112 |
+
word_usage = gr.Textbox(label="Word Usage", lines=3)
|
| 113 |
+
flow_structure = gr.Textbox(label="Flow & Structure", lines=3)
|
| 114 |
+
technical_soundness = gr.Textbox(label="Technical Soundness", lines=3)
|
| 115 |
+
|
| 116 |
+
with gr.Column():
|
| 117 |
+
reference_links = gr.Textbox(label="Reference Links", lines=3)
|
| 118 |
+
faqs = gr.Textbox(label="FAQs", lines=3)
|
| 119 |
+
top_suggestions = gr.Textbox(label="Top 5 Suggestions", lines=5)
|
| 120 |
+
pre_editing_genai = gr.Textbox(label="PreEditing GenAI Content%", lines=1)
|
| 121 |
+
post_editing_genai = gr.Textbox(label="PostEditing GenAI Content%", lines=1)
|
| 122 |
+
|
| 123 |
+
# Edited article markdown
|
| 124 |
+
gr.Markdown("### Edited Article")
|
| 125 |
+
edited_article = gr.Markdown(label="Edited Article")
|
| 126 |
+
|
| 127 |
+
# Button to submit and process the article
|
| 128 |
+
submit_btn = gr.Button("Submit")
|
| 129 |
+
|
| 130 |
+
# Action on submit
|
| 131 |
+
submit_btn.click(
|
| 132 |
+
process_article,
|
| 133 |
+
inputs=[article_input],
|
| 134 |
+
outputs=[word_count, language_grammar, word_usage, flow_structure, technical_soundness,
|
| 135 |
+
reference_links, faqs, top_suggestions, edited_article, pre_editing_genai, post_editing_genai]
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
# Launch the app
|
| 139 |
+
# demo.launch()
|