AV-AV commited on
Commit
2ac32c2
·
verified ·
1 Parent(s): 369b0f9

Update app.py

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