import openai
import os
import gradio as gr
import json
with open('mykey.txt', 'r') as file:
openai_key = file.read()
os.environ['OPENAI_API_KEY'] = openai_key
def agent(messages, model="gpt-4o-2024-08-06", 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_1 = f"""
You are a Blog Editor at Analytics Vidhya, an edtech platform focused on creating learning content related to Data Science, AI, Machine Learning, and especially Generative AI. Your task is to critically evaluate and edit blogs based on the following criteria:
1) **Language & Grammar**: Ensure correct grammar, sentence structure, and overall readability.
2) **Word Usage**: Check for proper vocabulary, terminology, and accurate word choice.
3) **Flow & Structure**: Ensure logical progression, coherent structure, and readability.
4) **Technical Soundness**: Verify accuracy of technical content and ensure clear explanation of concepts.
5) **Reference Links**: Confirm the presence of appropriate reference links, especially when third-party information is mentioned.
6) **FAQs**: Assess whether FAQs are relevant, non-repetitive, and useful to the article.
For each parameter, rate the blog on a scale of 1 to 10 and provide **specific suggestions** for improvement with examples. Use the following JSON structure for each parameter:
- "Score": Rating out of 10
- "Suggestions": Specific feedback on how to improve
**Additional Requirements**:
- Provide the **Top 5 suggestions** for improving the article overall.
- Edit the article to incorporate your suggestions and ensure the final version is more than 90% human-written, even if the original text contains Generative AI content.
- Report the percentage of Generative AI content before and after editing.
- Finally, output everything in JSON format with these keys:
- "Word count"
- "Language & Grammar"
- "Word Usage"
- "Flow & Structure"
- "Technical Soundness"
- "Reference Links"
- "FAQs"
- "Top5 Suggestions"
- "Edited Article"
- "PreEdit GenAI Perc"
- "PostEdit GenAI Perc"
Ensure the output is formatted correctly for use with the Python `json` library.
"""
example = """
~~This text has been removed from the original article.~~
This new text is added in the edited article.
"""
system_message_2 = f"""
You are a text comparison tool designed to compare the original article with the edited version and highlight the differences between them.
Your task is to:
1. **Highlight deletions** in the original article with a red strikethrough using Markdown and HTML.
2. **Highlight additions** in the edited article using green text.
Format your output using Markdown with embedded HTML tags, which can be displayed using Ipython's Markdown function.
- **Deletions**: Mark text removed from the original article with a red strikethrough, like this:
`~~Deleted text~~`.
- **Additions**: Mark text added in the edited article with green text, like this:
`Added text`.
Example format:
{example}
Make sure to apply these formatting rules consistently throughout the entire comparison.
"""
# Function to process all parameters including highlighted response
def process_all_parameters(article):
messages = [
{'role': 'system', 'content': system_message_1},
{'role': 'user', 'content': article}
]
response = agent(messages)
clean_response = response.strip('```json').strip('```')
try:
parsed_response = json.loads(clean_response)
except json.JSONDecodeError:
return ["Invalid response received from OpenAI"] * 11
# 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)
pre_editing_genai = json.dumps(parsed_response.get('PreEdit GenAI Perc', {}), indent=2)
post_editing_genai = json.dumps(parsed_response.get('PostEdit GenAI Perc', {}), indent=2)
edited_article = parsed_response.get('Edited Article', "No edited article found")
# Process highlighted response
messages2 = [
{'role': 'system', 'content': system_message_2},
{'role': 'user', 'content': f"This is the original article: {article}. This is the edited article: {edited_article}"}
]
highlighted_response = agent(messages2)
return (word_count, language_grammar, word_usage, flow_structure, technical_soundness,reference_links,
faqs, top_suggestions, pre_editing_genai, post_editing_genai, edited_article, highlighted_response)
# Gradio Interface
with gr.Blocks() as demo:
gr.Markdown("## AV Blog Editor V3")
gr.Markdown("### Guidelines")
gr.Markdown("""
1. Use your discretion while using this tool.
2. The suggestions will appear first and then the edited article with highlights. Expected run time: 3 min
3. The percentage of GPT generated content will not match what zerogpt.com says.
4. Do share any suggestions for improvements in this tool with Apoorv. You can get some candies for that :)
""")
article_input = gr.Textbox(label="Input Article", lines=12, placeholder="Enter your article here...")
submit_btn = gr.Button("Submit")
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="PreEdit GenAI Perc", lines=1)
post_editing_genai = gr.Textbox(label="PostEdit GenAI Perc", lines=1)
gr.Markdown("### Edits Highlighted")
highlighted_response = gr.Markdown()
edited_article = gr.Textbox(label="Edited Article")
# Handle everything in one go
def process(article):
return process_all_parameters(article)
# Process the article and get all outputs
submit_btn.click(
process,
inputs=[article_input],
outputs=[
word_count, language_grammar, word_usage, flow_structure, technical_soundness,
reference_links, faqs, top_suggestions, pre_editing_genai, post_editing_genai,
edited_article, highlighted_response
]
)
# Launch the app
demo.launch()