File size: 7,608 Bytes
32af946
 
b3013e5
2ac32c2
b3013e5
32af946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b61851
 
32af946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ac32c2
 
b3013e5
2ac32c2
b3013e5
 
 
2ac32c2
b3013e5
2ac32c2
b3013e5
 
 
2ac32c2
b3013e5
 
 
 
 
 
 
 
 
 
9b61851
 
b3013e5
 
2ac32c2
 
 
 
 
 
 
 
9b61851
 
b3013e5
2ac32c2
b3013e5
9b61851
2ac32c2
b3013e5
2ac32c2
 
 
 
b3013e5
 
9b61851
b3013e5
9b61851
 
b3013e5
 
 
 
 
 
 
 
 
 
 
 
9b61851
 
2ac32c2
b3013e5
9b61851
 
 
 
 
d9af52d
2ac32c2
 
 
 
 
4f2b762
2ac32c2
 
 
 
 
9b61851
2ac32c2
4f2b762
b3013e5
 
2ac32c2
cd2a5f7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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 = """
~~<span style="color:red">This text has been removed from the original article.</span>~~
<span style="color:green">This new text is added in the edited article.</span>
"""

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:
  `~~<span style="color:red">Deleted text</span>~~`.
- **Additions**: Mark text added in the edited article with green text, like this:
  `<span style="color:green">Added text</span>`.

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()