Spaces:
Sleeping
Sleeping
| import openai | |
| import os | |
| import json | |
| import gradio as gr | |
| with open('mykey.txt', 'r') as file: | |
| openai_key = file.read() | |
| os.environ['OPENAI_API_KEY'] = openai_key | |
| def get_completion_messages_gpt(messages, model="gpt-4o", temperature=0, presence_penalty=0, seed = 10, stream=False): | |
| response = openai.chat.completions.create( | |
| model=model, | |
| messages=messages, | |
| temperature=temperature, | |
| presence_penalty=presence_penalty, | |
| seed=seed, | |
| stream = stream | |
| ) | |
| return response.choices[0].message.content | |
| system_message_simple = f""" | |
| You are a Blog editor at Analytics Vidhya. Analytics Vidhya is an edtech platform focussing on creating | |
| learning content for topics related to Data science, AI, Machine Learning and especially Generative AI.\ | |
| You crtically evaluate for the following parameters in the blogs you edit:\ | |
| 1) Appropriate language and grammar structure | |
| 2) Correct word usage | |
| 3) Flow and struture of the article | |
| 4) Technical soundness of the article | |
| 5) Whether appropriate reference links are provided wherever required, especially where third party information is mentioned | |
| 6) FAQs are relevant to the article and non-repetetive | |
| Rate the article on these parameters on a scale of 10 and provide suggestions to improve the respective parameter scores with \ | |
| specific suggestions. | |
| For the parameters - use the keys mentioned in the json format below. | |
| For the above parameters use sub-keys - Score and Suggestions, to show the respective output. | |
| Also provide Top 5 suggestions for improvement in the article. | |
| Finally provide the edited article incorporating all the suggestions. \ | |
| While editing the article also check whether the content of the article is human written or generated by generative AI tools like chatGPT. | |
| Articles which have a high percentage of generative AI content rank lower on SEO, so your goal is to edit articles in a way\ | |
| that the artcle is more than 90% human written even after editing. Give the percentatge of content written by Generative AI\ | |
| before and after editing. | |
| Compare the original article with the edited article and in the edited article italicise the changes made compared \ | |
| to the original article using markdown and mention the text which should be hyperlinked with a reference link within double exclamation marks. | |
| Provide your output in JSON format with the keys: | |
| Word count, Language & Grammar, Word Usage, Flow & Structure, Technical Soundness, Reference Links, FAQs, Top5 Suggestions, Edited Article, \ | |
| PreEditing GenAi Content%, PreEditing GenAi Content%. | |
| Format the output such that the output can be directly used by python json library. | |
| """ | |
| # Function to process the input article | |
| def process_article(article): | |
| messages = [ | |
| {'role': 'system', 'content': system_message_simple}, | |
| {'role': 'user', 'content': article} | |
| ] | |
| response = get_completion_messages_gpt(messages) | |
| clean_response = response.strip('```json').strip('```') | |
| try: | |
| parsed_response = json.loads(clean_response) | |
| except json.JSONDecodeError: | |
| return ["Invalid response received from OpenAI"] * 10 | |
| # Extract different parts of the response | |
| word_count = json.dumps(parsed_response.get('Word count', {}), indent=2) | |
| language_grammar = json.dumps(parsed_response.get('Language & Grammar', {}), indent=2) | |
| word_usage = json.dumps(parsed_response.get('Word Usage', {}), indent=2) | |
| flow_structure = json.dumps(parsed_response.get('Flow & Structure', {}), indent=2) | |
| technical_soundness = json.dumps(parsed_response.get('Technical Soundness', {}), indent=2) | |
| reference_links = json.dumps(parsed_response.get('Reference Links', {}), indent=2) | |
| faqs = json.dumps(parsed_response.get('FAQs', {}), indent=2) | |
| top_suggestions = json.dumps(parsed_response.get('Top5 Suggestions', []), indent=2) | |
| edited_article = parsed_response.get('Edited Article', "No edited article found") | |
| pre_editing_genai = json.dumps(parsed_response.get('PreEditing GenAi Content%', {}), indent=2) | |
| post_editing_genai = json.dumps(parsed_response.get('PostEditing GenAi Content%', {}), indent=2) | |
| return (word_count, language_grammar, word_usage, flow_structure, technical_soundness, | |
| reference_links, faqs, top_suggestions, edited_article, pre_editing_genai, post_editing_genai) | |
| # Gradio Interface with improved layout | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## AV Blog Editor V1") | |
| gr.Markdown("### Guidelines (make sure to read these before using)") | |
| gr.Markdown(""" | |
| 1. Use your discretion while using this tool | |
| 2. The italicised content in the edited article represents the changes made by GPT. Make sure to review the changes. | |
| 3. The text under !! in the edited article shows approximate areas where reference link is required. The link it shows may not be correct. | |
| 4. The score and suggestions of various sections are good pointers to reflect back on your article. | |
| 5. The percentage of GPT generated content will not match what zerogpt.com says. Go with what zerogpt says. | |
| 6. Make sure to check zerogpt before using the edited article | |
| 7. Do share any suggestions for improvements in this tool with Apoorv. You can get some candies for that :) | |
| """) | |
| # Input article in a single row | |
| with gr.Row(): | |
| article_input = gr.Textbox(label="Input Article", lines=15, placeholder="Enter your article here...") | |
| # Outputs in two columns | |
| with gr.Row(): | |
| with gr.Column(): | |
| word_count = gr.Textbox(label="Word Count") | |
| language_grammar = gr.Textbox(label="Language & Grammar", lines=3) | |
| word_usage = gr.Textbox(label="Word Usage", lines=3) | |
| flow_structure = gr.Textbox(label="Flow & Structure", lines=3) | |
| technical_soundness = gr.Textbox(label="Technical Soundness", lines=3) | |
| with gr.Column(): | |
| reference_links = gr.Textbox(label="Reference Links", lines=3) | |
| faqs = gr.Textbox(label="FAQs", lines=3) | |
| top_suggestions = gr.Textbox(label="Top 5 Suggestions", lines=5) | |
| pre_editing_genai = gr.Textbox(label="PreEditing GenAI Content%", lines=1) | |
| post_editing_genai = gr.Textbox(label="PostEditing GenAI Content%", lines=1) | |
| # Edited article markdown | |
| gr.Markdown("### Edited Article") | |
| edited_article = gr.Markdown(label="Edited Article") | |
| # Button to submit and process the article | |
| submit_btn = gr.Button("Submit") | |
| # Action on submit | |
| submit_btn.click( | |
| process_article, | |
| inputs=[article_input], | |
| outputs=[word_count, language_grammar, word_usage, flow_structure, technical_soundness, | |
| reference_links, faqs, top_suggestions, edited_article, pre_editing_genai, post_editing_genai] | |
| ) | |
| # Launch the app | |
| demo.launch() |