Spaces:
Runtime error
Runtime error
eljanmahammadli
commited on
Commit
·
ba91632
1
Parent(s):
7c04c25
fixes to inline citation, html; added debug true, chroma db clean cache
Browse files- .gitignore +4 -1
- ai_generate.py +3 -0
- app.py +517 -461
.gitignore
CHANGED
@@ -5,4 +5,7 @@ nohup.out
|
|
5 |
*.log
|
6 |
*.json
|
7 |
*.pdf
|
8 |
-
temp.py
|
|
|
|
|
|
|
|
5 |
*.log
|
6 |
*.json
|
7 |
*.pdf
|
8 |
+
temp.py
|
9 |
+
temp.ipynb
|
10 |
+
chroma_db/
|
11 |
+
temp.txt
|
ai_generate.py
CHANGED
@@ -247,6 +247,9 @@ def generate_rag(
|
|
247 |
rag_chain = RunnablePassthrough.assign(context=lambda _: formatted_docs) | xml_prompt | llm | XMLOutputParser()
|
248 |
result = rag_chain.invoke({"input": prompt})
|
249 |
citations = get_citations(result, docs)
|
|
|
|
|
|
|
250 |
return result, citations
|
251 |
|
252 |
|
|
|
247 |
rag_chain = RunnablePassthrough.assign(context=lambda _: formatted_docs) | xml_prompt | llm | XMLOutputParser()
|
248 |
result = rag_chain.invoke({"input": prompt})
|
249 |
citations = get_citations(result, docs)
|
250 |
+
|
251 |
+
db.delete_collection() # delete othewise there could be duplicates because of the cache
|
252 |
+
|
253 |
return result, citations
|
254 |
|
255 |
|
app.py
CHANGED
@@ -4,48 +4,54 @@ export GOOGLE_APPLICATION_CREDENTIALS="gcp_creds.json"
|
|
4 |
"""
|
5 |
|
6 |
import re
|
|
|
|
|
7 |
from typing import Dict
|
8 |
from collections import defaultdict
|
9 |
from datetime import date, datetime
|
10 |
|
11 |
-
import gradio as gr
|
12 |
import nltk
|
13 |
import torch
|
14 |
import numpy as np
|
15 |
-
|
16 |
import language_tool_python
|
|
|
17 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
18 |
|
19 |
-
|
20 |
-
from
|
21 |
-
from
|
22 |
-
from
|
23 |
-
import
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
from datetime import datetime
|
28 |
-
from google.cloud import storage
|
29 |
|
30 |
-
|
31 |
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
"polygraf-ai/bc-roberta-openai-2sent"
|
37 |
-
).to(device),
|
38 |
-
"Polygraf AI (Advanced Model)": AutoModelForSequenceClassification.from_pretrained(
|
39 |
-
"polygraf-ai/bc_combined_3sent"
|
40 |
-
).to(device),
|
41 |
-
}
|
42 |
-
tokenizers = {
|
43 |
-
"Polygraf AI (Base Model)": AutoTokenizer.from_pretrained("polygraf-ai/bc-roberta-openai-2sent"),
|
44 |
-
"Polygraf AI (Advanced Model)": AutoTokenizer.from_pretrained("polygraf-ai/bc_combined_3sent"),
|
45 |
-
}
|
46 |
|
47 |
-
#
|
48 |
-
|
|
|
|
|
|
|
|
|
49 |
|
50 |
|
51 |
def generate_cited_html(cited_text, citations: dict):
|
@@ -79,31 +85,15 @@ def generate_cited_html(cited_text, citations: dict):
|
|
79 |
position: absolute;
|
80 |
z-index: 1;
|
81 |
top: 100%;
|
82 |
-
left: 0;
|
83 |
background-color: #f9f9f9;
|
84 |
border: 1px solid #ddd;
|
85 |
-
padding:
|
86 |
border-radius: 4px;
|
87 |
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
|
88 |
width: calc(min(90vw, 400px));
|
89 |
max-height: calc(min(80vh, 300px));
|
90 |
overflow-y: auto;
|
91 |
-
transform: translateX(0); /* Default position */
|
92 |
}
|
93 |
-
.reference-container .reference-popup {
|
94 |
-
left: 50%;
|
95 |
-
transform: translateX(-50%); /* Center align popup horizontally by default */
|
96 |
-
}
|
97 |
-
.reference-container[data-align="right"] .reference-popup {
|
98 |
-
left: auto;
|
99 |
-
right: 0;
|
100 |
-
transform: translateX(-10%); /* Pull the popup slightly left when near right edge */
|
101 |
-
}
|
102 |
-
.reference-container[data-align="left"] .reference-popup {
|
103 |
-
left: 0;
|
104 |
-
right: auto;
|
105 |
-
transform: translateX(20%);
|
106 |
-
}
|
107 |
.reference-popup .close-btn {
|
108 |
float: right;
|
109 |
cursor: pointer;
|
@@ -131,20 +121,53 @@ def generate_cited_html(cited_text, citations: dict):
|
|
131 |
input[type="radio"]:checked + .reference-popup {
|
132 |
display: block;
|
133 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
@media (prefers-color-scheme: dark) {
|
135 |
.reference-btn {
|
136 |
-
|
137 |
}
|
138 |
.reference-popup {
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
}
|
143 |
.reference-popup .close-btn {
|
144 |
-
|
145 |
}
|
146 |
.reference-popup .close-btn:hover {
|
147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
}
|
149 |
}
|
150 |
</style>
|
@@ -152,22 +175,46 @@ def generate_cited_html(cited_text, citations: dict):
|
|
152 |
document.addEventListener('click', (event) => {
|
153 |
const containers = document.querySelectorAll('.reference-container');
|
154 |
containers.forEach(container => {
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
});
|
164 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
</script>
|
166 |
<div style="height: 600px; overflow-y: auto; overflow-x: auto;">
|
167 |
"""
|
168 |
|
169 |
# Function to replace each citation with a reference button
|
|
|
|
|
170 |
def replace_citations(match):
|
|
|
171 |
citation_id = match.group(1) # Extract citation number from the match
|
172 |
ref_data = citations.get(int(citation_id))
|
173 |
|
@@ -175,19 +222,32 @@ def generate_cited_html(cited_text, citations: dict):
|
|
175 |
if not ref_data:
|
176 |
return match.group(0)
|
177 |
|
178 |
-
#
|
179 |
-
|
180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
|
182 |
-
#
|
|
|
|
|
|
|
|
|
183 |
button_html = f"""
|
184 |
<span class="reference-container">
|
185 |
-
<label for="ref-toggle-{
|
186 |
-
<input type="radio" id="ref-toggle-{
|
187 |
<span class="reference-popup">
|
188 |
-
<span class="close-btn" onclick="document.getElementById('ref-toggle-{
|
189 |
-
<strong>Source:</strong> {
|
190 |
-
<strong>Content:</strong> {
|
191 |
</span>
|
192 |
</span>
|
193 |
"""
|
@@ -356,13 +416,6 @@ ai_check_options = [
|
|
356 |
]
|
357 |
|
358 |
|
359 |
-
MC_TOKEN_SIZE = 256
|
360 |
-
TEXT_MC_MODEL_PATH = "polygraf-ai/mc-model"
|
361 |
-
MC_LABEL_MAP = ["OpenAI GPT", "Mistral", "CLAUDE", "Gemini", "Grammar Enhancer"]
|
362 |
-
text_mc_tokenizer = AutoTokenizer.from_pretrained(TEXT_MC_MODEL_PATH)
|
363 |
-
text_mc_model = AutoModelForSequenceClassification.from_pretrained(TEXT_MC_MODEL_PATH).to(device)
|
364 |
-
|
365 |
-
|
366 |
def predict_mc(text):
|
367 |
with torch.no_grad():
|
368 |
text_mc_model.eval()
|
@@ -836,420 +889,423 @@ def generate_and_format(
|
|
836 |
return generate_cited_html(article, citations), history
|
837 |
|
838 |
|
839 |
-
def create_interface():
|
840 |
-
|
841 |
-
|
842 |
-
|
843 |
-
|
844 |
-
|
845 |
-
|
846 |
-
|
847 |
-
|
848 |
-
|
849 |
-
|
850 |
-
|
851 |
-
|
852 |
-
|
853 |
-
|
854 |
-
|
855 |
-
|
856 |
-
|
857 |
-
|
858 |
-
|
859 |
-
|
860 |
-
|
861 |
-
|
862 |
-
|
863 |
-
|
864 |
-
|
865 |
-
|
866 |
-
|
867 |
-
|
868 |
-
|
869 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
870 |
)
|
871 |
-
|
872 |
-
|
873 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
874 |
elem_classes="input-highlight-yellow",
|
875 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
876 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
877 |
with gr.Row():
|
878 |
-
|
879 |
-
|
880 |
-
|
881 |
-
|
882 |
-
"Blog post",
|
883 |
-
"Report",
|
884 |
-
"Research paper",
|
885 |
-
"News article",
|
886 |
-
"White paper",
|
887 |
-
"Email",
|
888 |
-
"LinkedIn post",
|
889 |
-
"X (Twitter) post",
|
890 |
-
"Instagram Video Content",
|
891 |
-
"TikTok Video Content",
|
892 |
-
"Facebook post",
|
893 |
-
],
|
894 |
-
value="Article",
|
895 |
-
label="Format",
|
896 |
-
elem_classes="input-highlight-turquoise",
|
897 |
)
|
898 |
-
|
899 |
-
input_length = gr.Slider(
|
900 |
-
minimum=50,
|
901 |
-
maximum=5000,
|
902 |
-
step=50,
|
903 |
-
value=300,
|
904 |
-
label="Article Length",
|
905 |
-
elem_classes="input-highlight-pink",
|
906 |
-
)
|
907 |
-
|
908 |
with gr.Row():
|
909 |
-
|
910 |
-
|
911 |
-
|
912 |
-
"Informal",
|
913 |
-
"Technical",
|
914 |
-
"Conversational",
|
915 |
-
"Journalistic",
|
916 |
-
"Academic",
|
917 |
-
"Creative",
|
918 |
-
],
|
919 |
-
value="Formal",
|
920 |
-
label="Writing Style",
|
921 |
elem_classes="input-highlight-yellow",
|
922 |
)
|
923 |
-
|
924 |
-
|
925 |
-
|
926 |
-
|
927 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
928 |
)
|
|
|
|
|
929 |
|
930 |
-
|
931 |
-
|
932 |
-
|
933 |
-
"
|
934 |
-
|
935 |
-
|
936 |
-
|
937 |
-
|
938 |
-
]
|
939 |
-
|
940 |
-
|
941 |
-
|
942 |
-
|
943 |
-
|
944 |
-
|
945 |
-
|
946 |
-
|
947 |
-
|
948 |
-
|
949 |
-
|
950 |
-
|
951 |
-
|
952 |
-
|
953 |
-
|
954 |
-
|
955 |
-
|
956 |
-
|
957 |
-
|
958 |
-
|
959 |
-
|
960 |
-
|
961 |
-
|
962 |
-
|
963 |
-
|
964 |
-
|
965 |
-
|
966 |
-
|
967 |
-
|
968 |
-
|
969 |
-
|
970 |
-
|
971 |
-
|
972 |
-
|
973 |
-
"Expert interviews",
|
974 |
-
"Case studies",
|
975 |
-
],
|
976 |
-
value="News outlets",
|
977 |
-
label="References",
|
978 |
-
elem_classes="input-highlight-pink",
|
979 |
-
)
|
980 |
-
input_num_examples = gr.Dropdown(
|
981 |
-
choices=["1-2", "3-4", "5+"],
|
982 |
-
value="1-2",
|
983 |
-
label="Number of Examples/Case Studies",
|
984 |
-
elem_classes="input-highlight-yellow",
|
985 |
-
)
|
986 |
-
input_conclusion = gr.Dropdown(
|
987 |
-
choices=["Summary", "Call to Action", "Future Outlook", "Thought-provoking Question"],
|
988 |
-
value="Call to Action",
|
989 |
-
label="Conclusion Type",
|
990 |
-
elem_classes="input-highlight-turquoise",
|
991 |
)
|
992 |
-
gr.
|
993 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
994 |
with gr.Row():
|
995 |
-
|
996 |
-
|
|
|
|
|
|
|
997 |
)
|
998 |
-
|
999 |
-
|
1000 |
-
|
1001 |
-
|
1002 |
-
|
1003 |
-
|
1004 |
-
|
1005 |
-
|
1006 |
-
|
1007 |
-
|
1008 |
-
|
1009 |
-
|
1010 |
-
|
1011 |
-
|
1012 |
-
domains_to_include = gr.Dropdown(
|
1013 |
-
domain_list,
|
1014 |
-
value=domain_list,
|
1015 |
-
multiselect=True,
|
1016 |
-
label="Domains To Include",
|
1017 |
-
)
|
1018 |
-
with gr.Row():
|
1019 |
-
month_from = gr.Dropdown(
|
1020 |
-
choices=months,
|
1021 |
-
label="From Month",
|
1022 |
-
value="January",
|
1023 |
-
interactive=True,
|
1024 |
-
)
|
1025 |
-
day_from = gr.Textbox(label="From Day", value="01")
|
1026 |
-
year_from = gr.Textbox(label="From Year", value="2000")
|
1027 |
-
|
1028 |
-
with gr.Row():
|
1029 |
-
month_to = gr.Dropdown(
|
1030 |
-
choices=months,
|
1031 |
-
label="To Month",
|
1032 |
-
value=d1[1],
|
1033 |
-
interactive=True,
|
1034 |
-
)
|
1035 |
-
day_to = gr.Textbox(label="To Day", value=d1[0])
|
1036 |
-
year_to = gr.Textbox(label="To Year", value=d1[2])
|
1037 |
-
|
1038 |
-
gr.Markdown("# Add Optional PDF Files with Information", elem_classes="text-center text-3xl mb-6")
|
1039 |
-
pdf_file_input = gr.File(label="Upload PDF(s)", file_count="multiple", file_types=[".pdf"])
|
1040 |
"""
|
1041 |
-
# NOTE:
|
1042 |
-
|
1043 |
-
|
1044 |
-
ai_generator = gr.Dropdown(
|
1045 |
-
choices=[
|
1046 |
-
"OpenAI GPT 4",
|
1047 |
-
"OpenAI GPT 4o",
|
1048 |
-
"OpenAI GPT 4o Mini",
|
1049 |
-
"Claude Sonnet 3.5",
|
1050 |
-
"Gemini 1.5 Pro",
|
1051 |
-
"LLaMA 3",
|
1052 |
-
],
|
1053 |
-
value="OpenAI GPT 4o Mini",
|
1054 |
-
label="AI Model",
|
1055 |
-
elem_classes="input-highlight-pink",
|
1056 |
-
)
|
1057 |
-
input_api = gr.Textbox(label="API Key", visible=False)
|
1058 |
-
ai_generator.change(update_visibility_api, ai_generator, input_api)
|
1059 |
"""
|
1060 |
-
generate_btn = gr.Button("Generate Article", variant="primary")
|
1061 |
|
1062 |
-
|
1063 |
-
|
1064 |
-
|
1065 |
-
|
1066 |
-
|
1067 |
-
)
|
1068 |
-
humanizer_feedback = gr.Textbox(label="Add optional feedback on humanizer")
|
1069 |
-
report_humanized_btn = gr.Button("Report Humanized Text", variant="primary", visible=True)
|
1070 |
-
with gr.Accordion("Regenerate Article", open=False):
|
1071 |
-
ai_comments = gr.Textbox(
|
1072 |
-
label="Add comments to help edit generated text", interactive=True, visible=True
|
1073 |
-
)
|
1074 |
-
regenerate_btn = gr.Button("Regenerate Article", variant="primary", visible=True)
|
1075 |
|
1076 |
-
|
1077 |
-
|
1078 |
-
|
1079 |
-
|
1080 |
-
|
1081 |
-
with gr.Accordion("AI Detection Results", open=True):
|
1082 |
-
ai_check_result = gr.Label(label="AI Check Result")
|
1083 |
-
mc_check_result = gr.Label(label="Creator Check Result")
|
1084 |
-
highlighted_text = gr.HTML(label="Sentence Breakdown", visible=False)
|
1085 |
-
|
1086 |
-
with gr.Accordion("Advanced Humanizer Settings", open=False):
|
1087 |
-
with gr.Row():
|
1088 |
-
model_dropdown = gr.Radio(
|
1089 |
-
choices=["Standard Model", "Advanced Model (Beta)"],
|
1090 |
-
value="Advanced Model (Beta)",
|
1091 |
-
label="Humanizer Model Version",
|
1092 |
-
)
|
1093 |
-
with gr.Row():
|
1094 |
-
temperature_slider = gr.Slider(
|
1095 |
-
minimum=0.5, maximum=2.0, step=0.1, value=1.0, label="Temperature"
|
1096 |
-
)
|
1097 |
-
top_k_slider = gr.Slider(minimum=0, maximum=300, step=25, value=40, label="Top k")
|
1098 |
-
with gr.Row():
|
1099 |
-
repetition_penalty_slider = gr.Slider(
|
1100 |
-
minimum=1.0, maximum=2.0, step=0.1, value=1, label="Repetition Penalty"
|
1101 |
-
)
|
1102 |
-
length_penalty_slider = gr.Slider(
|
1103 |
-
minimum=0.0, maximum=2.0, step=0.1, value=1.0, label="Length Penalty"
|
1104 |
-
)
|
1105 |
-
|
1106 |
-
humanize_btn = gr.Button("Humanize")
|
1107 |
-
# humanized_output = gr.Markdown(label="Humanized Article", value="\n\n\n\n", render=True)
|
1108 |
-
# copy_to_input_btn = gr.Button("Copy to Input for AI Check")
|
1109 |
-
|
1110 |
-
with gr.Tab("History"):
|
1111 |
-
history_chat = gr.Chatbot(label="Generation History", height=1000)
|
1112 |
-
clear_history_btn = gr.Button("Clear History")
|
1113 |
-
clear_history_btn.click(clear_history, outputs=[history, history_chat])
|
1114 |
-
"""
|
1115 |
-
# NOTE: REMOVED REFRESH BUTTON
|
1116 |
-
refresh_button = gr.Button("Refresh History")
|
1117 |
-
refresh_button.click(get_history, outputs=history_chat)
|
1118 |
-
"""
|
1119 |
-
|
1120 |
-
def regenerate_visible(text):
|
1121 |
-
if text:
|
1122 |
-
return gr.update(visible=True)
|
1123 |
-
else:
|
1124 |
-
return gr.update(visible=False)
|
1125 |
-
|
1126 |
-
def highlight_visible(text):
|
1127 |
-
if text.startswith("Polygraf"):
|
1128 |
-
return gr.update(visible=True)
|
1129 |
-
else:
|
1130 |
-
return gr.update(visible=False)
|
1131 |
-
|
1132 |
-
def search_visible(toggle):
|
1133 |
-
if toggle:
|
1134 |
-
return gr.update(visible=True)
|
1135 |
-
else:
|
1136 |
-
return gr.update(visible=False)
|
1137 |
-
|
1138 |
-
google_search_check.change(search_visible, inputs=google_search_check, outputs=search_options)
|
1139 |
-
# ai_detector_dropdown.change(highlight_visible, inputs=ai_detector_dropdown, outputs=highlighted_text)
|
1140 |
-
# output_article.change(regenerate_visible, inputs=output_article, outputs=ai_comments)
|
1141 |
-
# ai_comments.change(regenerate_visible, inputs=output_article, outputs=regenerate_btn)
|
1142 |
-
ai_check_btn.click(highlight_visible, inputs=ai_detector_dropdown, outputs=highlighted_text)
|
1143 |
-
|
1144 |
-
# Update the default structure based on the selected format
|
1145 |
-
# e.g. "Plain Text" for certain formats
|
1146 |
-
input_format.change(fn=update_structure, inputs=input_format, outputs=input_structure)
|
1147 |
-
model_dropdown.change(fn=update_temperature, inputs=model_dropdown, outputs=temperature_slider)
|
1148 |
-
report_humanized_btn.click(
|
1149 |
-
save_humanizer_feedback_to_cloud_storage, inputs=[latest_humanizer_data, humanizer_feedback]
|
1150 |
-
)
|
1151 |
|
1152 |
-
|
1153 |
-
|
1154 |
-
|
1155 |
-
|
1156 |
-
|
1157 |
-
|
1158 |
-
|
1159 |
-
|
1160 |
-
|
1161 |
-
|
1162 |
-
|
1163 |
-
|
1164 |
-
|
1165 |
-
|
1166 |
-
|
1167 |
-
|
1168 |
-
|
1169 |
-
|
1170 |
-
|
1171 |
-
google_search_check,
|
1172 |
-
year_from,
|
1173 |
-
month_from,
|
1174 |
-
day_from,
|
1175 |
-
year_to,
|
1176 |
-
month_to,
|
1177 |
-
day_to,
|
1178 |
-
domains_to_include,
|
1179 |
-
include_sites,
|
1180 |
-
exclude_sites,
|
1181 |
-
pdf_file_input,
|
1182 |
-
history,
|
1183 |
-
],
|
1184 |
-
outputs=[output_article, history],
|
1185 |
-
)
|
1186 |
|
1187 |
-
|
1188 |
-
|
1189 |
-
|
1190 |
-
|
1191 |
-
|
1192 |
-
|
1193 |
-
|
1194 |
-
|
1195 |
-
|
1196 |
-
|
1197 |
-
|
1198 |
-
|
1199 |
-
|
1200 |
-
|
1201 |
-
|
1202 |
-
|
1203 |
-
|
1204 |
-
|
1205 |
-
|
1206 |
-
|
1207 |
-
|
1208 |
-
|
1209 |
-
|
1210 |
-
|
1211 |
-
|
1212 |
-
|
1213 |
-
|
1214 |
-
|
1215 |
-
|
1216 |
-
|
1217 |
-
|
1218 |
-
|
1219 |
-
|
1220 |
-
|
1221 |
-
outputs=[output_article, history],
|
1222 |
-
)
|
1223 |
|
1224 |
-
|
1225 |
-
|
1226 |
-
|
1227 |
-
|
1228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1229 |
|
1230 |
-
|
1231 |
-
|
1232 |
-
|
1233 |
-
|
1234 |
-
|
1235 |
-
|
1236 |
-
|
1237 |
-
|
1238 |
-
|
1239 |
-
|
1240 |
-
|
1241 |
-
|
1242 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
1243 |
|
1244 |
-
|
1245 |
-
|
1246 |
-
|
1247 |
|
1248 |
-
|
1249 |
|
1250 |
|
1251 |
if __name__ == "__main__":
|
1252 |
-
demo = create_interface()
|
1253 |
# demo.queue(
|
1254 |
# max_size=2,
|
1255 |
# default_concurrency_limit=2,
|
|
|
4 |
"""
|
5 |
|
6 |
import re
|
7 |
+
import uuid
|
8 |
+
import json
|
9 |
from typing import Dict
|
10 |
from collections import defaultdict
|
11 |
from datetime import date, datetime
|
12 |
|
|
|
13 |
import nltk
|
14 |
import torch
|
15 |
import numpy as np
|
16 |
+
import gradio as gr
|
17 |
import language_tool_python
|
18 |
+
from scipy.special import softmax
|
19 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
20 |
+
from google.cloud import storage
|
21 |
|
22 |
+
if gr.NO_RELOAD:
|
23 |
+
from humanize import humanize_text, device
|
24 |
+
from utils import remove_special_characters, split_text_allow_complete_sentences_nltk
|
25 |
+
from google_search import google_search, months, domain_list, build_date
|
26 |
+
from ai_generate import generate, citations_to_html, remove_citations, display_cited_text
|
27 |
|
28 |
+
if gr.NO_RELOAD:
|
29 |
+
nltk.download("punkt_tab")
|
|
|
|
|
30 |
|
31 |
+
print(f"Using device: {device}")
|
32 |
|
33 |
+
models = {
|
34 |
+
"Polygraf AI (Base Model)": AutoModelForSequenceClassification.from_pretrained(
|
35 |
+
"polygraf-ai/bc-roberta-openai-2sent"
|
36 |
+
).to(device),
|
37 |
+
"Polygraf AI (Advanced Model)": AutoModelForSequenceClassification.from_pretrained(
|
38 |
+
"polygraf-ai/bc_combined_3sent"
|
39 |
+
).to(device),
|
40 |
+
}
|
41 |
+
tokenizers = {
|
42 |
+
"Polygraf AI (Base Model)": AutoTokenizer.from_pretrained("polygraf-ai/bc-roberta-openai-2sent"),
|
43 |
+
"Polygraf AI (Advanced Model)": AutoTokenizer.from_pretrained("polygraf-ai/bc_combined_3sent"),
|
44 |
+
}
|
45 |
|
46 |
+
# grammar correction tool
|
47 |
+
tool = language_tool_python.LanguageTool("en-US")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
# source detection model
|
50 |
+
MC_TOKEN_SIZE = 256
|
51 |
+
TEXT_MC_MODEL_PATH = "polygraf-ai/mc-model"
|
52 |
+
MC_LABEL_MAP = ["OpenAI GPT", "Mistral", "CLAUDE", "Gemini", "Grammar Enhancer"]
|
53 |
+
text_mc_tokenizer = AutoTokenizer.from_pretrained(TEXT_MC_MODEL_PATH)
|
54 |
+
text_mc_model = AutoModelForSequenceClassification.from_pretrained(TEXT_MC_MODEL_PATH).to(device)
|
55 |
|
56 |
|
57 |
def generate_cited_html(cited_text, citations: dict):
|
|
|
85 |
position: absolute;
|
86 |
z-index: 1;
|
87 |
top: 100%;
|
|
|
88 |
background-color: #f9f9f9;
|
89 |
border: 1px solid #ddd;
|
90 |
+
padding: 15px;
|
91 |
border-radius: 4px;
|
92 |
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
|
93 |
width: calc(min(90vw, 400px));
|
94 |
max-height: calc(min(80vh, 300px));
|
95 |
overflow-y: auto;
|
|
|
96 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
.reference-popup .close-btn {
|
98 |
float: right;
|
99 |
cursor: pointer;
|
|
|
121 |
input[type="radio"]:checked + .reference-popup {
|
122 |
display: block;
|
123 |
}
|
124 |
+
|
125 |
+
/* Additional styling for distinct sections */
|
126 |
+
.reference-popup strong {
|
127 |
+
font-weight: bold;
|
128 |
+
color: #333;
|
129 |
+
display: block;
|
130 |
+
margin-bottom: 5px;
|
131 |
+
}
|
132 |
+
.reference-popup p {
|
133 |
+
margin: 0 0 10px 0;
|
134 |
+
padding: 0;
|
135 |
+
}
|
136 |
+
.reference-popup .source {
|
137 |
+
margin-bottom: 10px;
|
138 |
+
font-size: 14px;
|
139 |
+
font-weight: bold;
|
140 |
+
color: #1e90ff;
|
141 |
+
}
|
142 |
+
.reference-popup .content {
|
143 |
+
margin-bottom: 10px;
|
144 |
+
font-size: 13px;
|
145 |
+
color: #555;
|
146 |
+
}
|
147 |
+
|
148 |
@media (prefers-color-scheme: dark) {
|
149 |
.reference-btn {
|
150 |
+
background-color: #1e90ff;
|
151 |
}
|
152 |
.reference-popup {
|
153 |
+
background-color: #2c2c2c;
|
154 |
+
border-color: #444;
|
155 |
+
color: #f1f1f1;
|
156 |
}
|
157 |
.reference-popup .close-btn {
|
158 |
+
background-color: #ff4c4c;
|
159 |
}
|
160 |
.reference-popup .close-btn:hover {
|
161 |
+
background-color: #ff3333;
|
162 |
+
}
|
163 |
+
.reference-popup strong {
|
164 |
+
color: #ddd;
|
165 |
+
}
|
166 |
+
.reference-popup .source {
|
167 |
+
color: #1e90ff;
|
168 |
+
}
|
169 |
+
.reference-popup .content {
|
170 |
+
color: #bbb;
|
171 |
}
|
172 |
}
|
173 |
</style>
|
|
|
175 |
document.addEventListener('click', (event) => {
|
176 |
const containers = document.querySelectorAll('.reference-container');
|
177 |
containers.forEach(container => {
|
178 |
+
const rect = container.getBoundingClientRect();
|
179 |
+
const popup = container.querySelector('.reference-popup');
|
180 |
+
|
181 |
+
// Reset alignment
|
182 |
+
popup.style.left = '';
|
183 |
+
popup.style.right = '';
|
184 |
+
|
185 |
+
const popupWidth = popup.offsetWidth;
|
186 |
+
const viewportWidth = window.innerWidth;
|
187 |
+
|
188 |
+
// If the popup would go off the right edge
|
189 |
+
if (rect.right + popupWidth > viewportWidth) {
|
190 |
+
popup.style.right = '0'; // Align popup to the right
|
191 |
+
}
|
192 |
+
// If the popup would go off the left edge
|
193 |
+
else if (rect.left - popupWidth < 0) {
|
194 |
+
popup.style.left = '0'; // Align popup to the left
|
195 |
+
}
|
196 |
+
// Otherwise center it
|
197 |
+
else {
|
198 |
+
popup.style.left = '50%';
|
199 |
+
popup.style.transform = 'translateX(-50%)'; // Center the popup
|
200 |
+
}
|
201 |
});
|
202 |
});
|
203 |
+
|
204 |
+
function closeReferencePanes() {
|
205 |
+
document.querySelectorAll('input[name="reference"]').forEach((input) => {
|
206 |
+
input.checked = false;
|
207 |
+
});
|
208 |
+
}
|
209 |
</script>
|
210 |
<div style="height: 600px; overflow-y: auto; overflow-x: auto;">
|
211 |
"""
|
212 |
|
213 |
# Function to replace each citation with a reference button
|
214 |
+
citation_count = 0 # To track unique instances of each citation
|
215 |
+
|
216 |
def replace_citations(match):
|
217 |
+
nonlocal citation_count
|
218 |
citation_id = match.group(1) # Extract citation number from the match
|
219 |
ref_data = citations.get(int(citation_id))
|
220 |
|
|
|
222 |
if not ref_data:
|
223 |
return match.group(0)
|
224 |
|
225 |
+
# Getting PDF file from gradio path
|
226 |
+
if "/var/tmp/gradio/" in ref_data["source"]:
|
227 |
+
ref_data["source"] = ref_data["source"].split("/")[-1]
|
228 |
+
|
229 |
+
# remove new line artifacts from scraping / parsing
|
230 |
+
ref_data["content"] = ref_data["content"].replace("\n", " ")
|
231 |
+
|
232 |
+
# Check if source is a URL, make it clickable if so
|
233 |
+
if ref_data["source"].startswith("http"):
|
234 |
+
source_html = f'<a href="{ref_data["source"]}" target="_blank" class="source">{ref_data["source"]}</a>'
|
235 |
+
else:
|
236 |
+
source_html = f'<span class="source">{ref_data["source"]}</span>'
|
237 |
|
238 |
+
# Unique id for each reference button and popup
|
239 |
+
unique_id = f"{citation_id}-{citation_count}"
|
240 |
+
citation_count += 1
|
241 |
+
|
242 |
+
# HTML code for the reference button and popup with formatted content
|
243 |
button_html = f"""
|
244 |
<span class="reference-container">
|
245 |
+
<label for="ref-toggle-{unique_id}" class="reference-btn" onclick="closeReferencePanes(); document.getElementById('ref-toggle-{unique_id}').checked = true;">{int(citation_id)+1}</label>
|
246 |
+
<input type="radio" id="ref-toggle-{unique_id}" name="reference" />
|
247 |
<span class="reference-popup">
|
248 |
+
<span class="close-btn" onclick="document.getElementById('ref-toggle-{unique_id}').checked = false;">×</span>
|
249 |
+
<strong>Source:</strong> {source_html}
|
250 |
+
<strong>Content:</strong> <p class="content">{ref_data["content"]}</p>
|
251 |
</span>
|
252 |
</span>
|
253 |
"""
|
|
|
416 |
]
|
417 |
|
418 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
419 |
def predict_mc(text):
|
420 |
with torch.no_grad():
|
421 |
text_mc_model.eval()
|
|
|
889 |
return generate_cited_html(article, citations), history
|
890 |
|
891 |
|
892 |
+
# def create_interface():
|
893 |
+
with gr.Blocks(
|
894 |
+
theme=gr.themes.Default(
|
895 |
+
primary_hue=gr.themes.colors.pink, secondary_hue=gr.themes.colors.yellow, neutral_hue=gr.themes.colors.gray
|
896 |
+
),
|
897 |
+
css="""
|
898 |
+
.input-highlight-pink block_label {background-color: #008080}
|
899 |
+
""",
|
900 |
+
) as demo:
|
901 |
+
history = gr.State([])
|
902 |
+
latest_humanizer_data = gr.State()
|
903 |
+
today = date.today()
|
904 |
+
# dd/mm/YY
|
905 |
+
d1 = today.strftime("%d/%B/%Y")
|
906 |
+
d1 = d1.split("/")
|
907 |
+
gr.Markdown("# Polygraf AI Content Writer", elem_classes="text-center text-3xl mb-6")
|
908 |
+
|
909 |
+
with gr.Row():
|
910 |
+
with gr.Column(scale=1):
|
911 |
+
with gr.Group():
|
912 |
+
gr.Markdown("## Article Configuration", elem_classes="text-xl mb-4")
|
913 |
+
input_role = gr.Textbox(label="I am a", placeholder="Enter your role", value="Student")
|
914 |
+
input_topic = gr.Textbox(
|
915 |
+
label="Topic",
|
916 |
+
placeholder="Enter the main topic of your article",
|
917 |
+
elem_classes="input-highlight-pink",
|
918 |
+
)
|
919 |
+
input_context = gr.Textbox(
|
920 |
+
label="Context",
|
921 |
+
placeholder="Provide some context for your topic",
|
922 |
+
elem_classes="input-highlight-pink",
|
923 |
+
)
|
924 |
+
input_keywords = gr.Textbox(
|
925 |
+
label="Keywords",
|
926 |
+
placeholder="Enter comma-separated keywords",
|
927 |
+
elem_classes="input-highlight-yellow",
|
928 |
+
)
|
929 |
+
|
930 |
+
with gr.Row():
|
931 |
+
input_format = gr.Dropdown(
|
932 |
+
choices=[
|
933 |
+
"Article",
|
934 |
+
"Essay",
|
935 |
+
"Blog post",
|
936 |
+
"Report",
|
937 |
+
"Research paper",
|
938 |
+
"News article",
|
939 |
+
"White paper",
|
940 |
+
"Email",
|
941 |
+
"LinkedIn post",
|
942 |
+
"X (Twitter) post",
|
943 |
+
"Instagram Video Content",
|
944 |
+
"TikTok Video Content",
|
945 |
+
"Facebook post",
|
946 |
+
],
|
947 |
+
value="Article",
|
948 |
+
label="Format",
|
949 |
+
elem_classes="input-highlight-turquoise",
|
950 |
)
|
951 |
+
|
952 |
+
input_length = gr.Slider(
|
953 |
+
minimum=50,
|
954 |
+
maximum=5000,
|
955 |
+
step=50,
|
956 |
+
value=300,
|
957 |
+
label="Article Length",
|
958 |
+
elem_classes="input-highlight-pink",
|
959 |
+
)
|
960 |
+
|
961 |
+
with gr.Row():
|
962 |
+
input_writing_style = gr.Dropdown(
|
963 |
+
choices=[
|
964 |
+
"Formal",
|
965 |
+
"Informal",
|
966 |
+
"Technical",
|
967 |
+
"Conversational",
|
968 |
+
"Journalistic",
|
969 |
+
"Academic",
|
970 |
+
"Creative",
|
971 |
+
],
|
972 |
+
value="Formal",
|
973 |
+
label="Writing Style",
|
974 |
elem_classes="input-highlight-yellow",
|
975 |
)
|
976 |
+
input_tone = gr.Dropdown(
|
977 |
+
choices=["Friendly", "Professional", "Neutral", "Enthusiastic", "Skeptical", "Humorous"],
|
978 |
+
value="Professional",
|
979 |
+
label="Tone",
|
980 |
+
elem_classes="input-highlight-turquoise",
|
981 |
+
)
|
982 |
|
983 |
+
input_user_category = gr.Dropdown(
|
984 |
+
choices=[
|
985 |
+
"Students",
|
986 |
+
"Professionals",
|
987 |
+
"Researchers",
|
988 |
+
"General Public",
|
989 |
+
"Policymakers",
|
990 |
+
"Entrepreneurs",
|
991 |
+
],
|
992 |
+
value="General Public",
|
993 |
+
label="Target Audience",
|
994 |
+
elem_classes="input-highlight-pink",
|
995 |
+
)
|
996 |
+
input_depth = gr.Dropdown(
|
997 |
+
choices=[
|
998 |
+
"Surface-level overview",
|
999 |
+
"Moderate analysis",
|
1000 |
+
"In-depth research",
|
1001 |
+
"Comprehensive study",
|
1002 |
+
],
|
1003 |
+
value="Moderate analysis",
|
1004 |
+
label="Depth of Content",
|
1005 |
+
elem_classes="input-highlight-yellow",
|
1006 |
+
)
|
1007 |
+
input_structure = gr.Dropdown(
|
1008 |
+
choices=[
|
1009 |
+
"Introduction, Body, Conclusion",
|
1010 |
+
"Abstract, Introduction, Methods, Results, Discussion, Conclusion",
|
1011 |
+
"Executive Summary, Problem Statement, Analysis, Recommendations, Conclusion",
|
1012 |
+
"Introduction, Literature Review, Methodology, Findings, Analysis, Conclusion",
|
1013 |
+
"Plain Text",
|
1014 |
+
],
|
1015 |
+
value="Introduction, Body, Conclusion",
|
1016 |
+
label="Structure",
|
1017 |
+
elem_classes="input-highlight-turquoise",
|
1018 |
+
interactive=True,
|
1019 |
+
)
|
1020 |
+
input_references = gr.Dropdown(
|
1021 |
+
choices=[
|
1022 |
+
"Academic journals",
|
1023 |
+
"Industry reports",
|
1024 |
+
"Government publications",
|
1025 |
+
"News outlets",
|
1026 |
+
"Expert interviews",
|
1027 |
+
"Case studies",
|
1028 |
+
],
|
1029 |
+
value="News outlets",
|
1030 |
+
label="References",
|
1031 |
+
elem_classes="input-highlight-pink",
|
1032 |
+
)
|
1033 |
+
input_num_examples = gr.Dropdown(
|
1034 |
+
choices=["1-2", "3-4", "5+"],
|
1035 |
+
value="1-2",
|
1036 |
+
label="Number of Examples/Case Studies",
|
1037 |
+
elem_classes="input-highlight-yellow",
|
1038 |
+
)
|
1039 |
+
input_conclusion = gr.Dropdown(
|
1040 |
+
choices=["Summary", "Call to Action", "Future Outlook", "Thought-provoking Question"],
|
1041 |
+
value="Call to Action",
|
1042 |
+
label="Conclusion Type",
|
1043 |
+
elem_classes="input-highlight-turquoise",
|
1044 |
+
)
|
1045 |
+
gr.Markdown("# Search Options", elem_classes="text-center text-3xl mb-6")
|
1046 |
+
google_default = False
|
1047 |
+
with gr.Row():
|
1048 |
+
google_search_check = gr.Checkbox(
|
1049 |
+
label="Enable Internet Search For Recent Sources", value=google_default
|
1050 |
+
)
|
1051 |
+
with gr.Group(visible=google_default) as search_options:
|
1052 |
with gr.Row():
|
1053 |
+
include_sites = gr.Textbox(
|
1054 |
+
label="Include Specific Websites",
|
1055 |
+
placeholder="Enter comma-separated keywords",
|
1056 |
+
elem_classes="input-highlight-yellow",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1057 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1058 |
with gr.Row():
|
1059 |
+
exclude_sites = gr.Textbox(
|
1060 |
+
label="Exclude Specific Websites",
|
1061 |
+
placeholder="Enter comma-separated keywords",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1062 |
elem_classes="input-highlight-yellow",
|
1063 |
)
|
1064 |
+
with gr.Row():
|
1065 |
+
domains_to_include = gr.Dropdown(
|
1066 |
+
domain_list,
|
1067 |
+
value=domain_list,
|
1068 |
+
multiselect=True,
|
1069 |
+
label="Domains To Include",
|
1070 |
+
)
|
1071 |
+
with gr.Row():
|
1072 |
+
month_from = gr.Dropdown(
|
1073 |
+
choices=months,
|
1074 |
+
label="From Month",
|
1075 |
+
value="January",
|
1076 |
+
interactive=True,
|
1077 |
)
|
1078 |
+
day_from = gr.Textbox(label="From Day", value="01")
|
1079 |
+
year_from = gr.Textbox(label="From Year", value="2000")
|
1080 |
|
1081 |
+
with gr.Row():
|
1082 |
+
month_to = gr.Dropdown(
|
1083 |
+
choices=months,
|
1084 |
+
label="To Month",
|
1085 |
+
value=d1[1],
|
1086 |
+
interactive=True,
|
1087 |
+
)
|
1088 |
+
day_to = gr.Textbox(label="To Day", value=d1[0])
|
1089 |
+
year_to = gr.Textbox(label="To Year", value=d1[2])
|
1090 |
+
|
1091 |
+
gr.Markdown("# Add Optional PDF Files with Information", elem_classes="text-center text-3xl mb-6")
|
1092 |
+
pdf_file_input = gr.File(label="Upload PDF(s)", file_count="multiple", file_types=[".pdf"])
|
1093 |
+
"""
|
1094 |
+
# NOTE: HIDE AI MODEL SELECTION
|
1095 |
+
with gr.Group():
|
1096 |
+
gr.Markdown("## AI Model Configuration", elem_classes="text-xl mb-4")
|
1097 |
+
ai_generator = gr.Dropdown(
|
1098 |
+
choices=[
|
1099 |
+
"OpenAI GPT 4",
|
1100 |
+
"OpenAI GPT 4o",
|
1101 |
+
"OpenAI GPT 4o Mini",
|
1102 |
+
"Claude Sonnet 3.5",
|
1103 |
+
"Gemini 1.5 Pro",
|
1104 |
+
"LLaMA 3",
|
1105 |
+
],
|
1106 |
+
value="OpenAI GPT 4o Mini",
|
1107 |
+
label="AI Model",
|
1108 |
+
elem_classes="input-highlight-pink",
|
1109 |
+
)
|
1110 |
+
input_api = gr.Textbox(label="API Key", visible=False)
|
1111 |
+
ai_generator.change(update_visibility_api, ai_generator, input_api)
|
1112 |
+
"""
|
1113 |
+
generate_btn = gr.Button("Generate Article", variant="primary")
|
1114 |
+
|
1115 |
+
with gr.Column(scale=2):
|
1116 |
+
with gr.Tab("Text Generator"):
|
1117 |
+
output_article = gr.HTML(
|
1118 |
+
value="""<div style="height: 600px;"></div>""",
|
1119 |
+
label="Generated Article",
|
1120 |
+
)
|
1121 |
+
with gr.Accordion("Regenerate Article", open=False):
|
1122 |
+
ai_comments = gr.Textbox(
|
1123 |
+
label="Add comments to help edit generated text", interactive=True, visible=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1124 |
)
|
1125 |
+
regenerate_btn = gr.Button("Regenerate Article", variant="primary", visible=True)
|
1126 |
+
|
1127 |
+
ai_detector_dropdown = gr.Dropdown(
|
1128 |
+
choices=ai_check_options, label="Select AI Detector", value="Polygraf AI (Base Model)"
|
1129 |
+
)
|
1130 |
+
ai_check_btn = gr.Button("AI Check")
|
1131 |
+
|
1132 |
+
with gr.Accordion("AI Detection Results", open=True):
|
1133 |
+
ai_check_result = gr.Label(label="AI Check Result")
|
1134 |
+
mc_check_result = gr.Label(label="Creator Check Result")
|
1135 |
+
highlighted_text = gr.HTML(label="Sentence Breakdown", visible=False)
|
1136 |
+
|
1137 |
+
with gr.Accordion("Advanced Humanizer Settings", open=False):
|
1138 |
+
with gr.Row():
|
1139 |
+
model_dropdown = gr.Radio(
|
1140 |
+
choices=["Standard Model", "Advanced Model (Beta)"],
|
1141 |
+
value="Advanced Model (Beta)",
|
1142 |
+
label="Humanizer Model Version",
|
1143 |
+
)
|
1144 |
+
with gr.Row():
|
1145 |
+
temperature_slider = gr.Slider(
|
1146 |
+
minimum=0.5, maximum=2.0, step=0.1, value=1.0, label="Temperature"
|
1147 |
+
)
|
1148 |
+
top_k_slider = gr.Slider(minimum=0, maximum=300, step=25, value=40, label="Top k")
|
1149 |
with gr.Row():
|
1150 |
+
repetition_penalty_slider = gr.Slider(
|
1151 |
+
minimum=1.0, maximum=2.0, step=0.1, value=1, label="Repetition Penalty"
|
1152 |
+
)
|
1153 |
+
length_penalty_slider = gr.Slider(
|
1154 |
+
minimum=0.0, maximum=2.0, step=0.1, value=1.0, label="Length Penalty"
|
1155 |
)
|
1156 |
+
|
1157 |
+
humanize_btn = gr.Button("Humanize")
|
1158 |
+
with gr.Row(equal_height=False):
|
1159 |
+
with gr.Column():
|
1160 |
+
humanizer_feedback = gr.Textbox(label="Add optional feedback on humanizer")
|
1161 |
+
with gr.Column():
|
1162 |
+
report_humanized_btn = gr.Button("Report Humanized Text", variant="primary", visible=True)
|
1163 |
+
# humanized_output = gr.Markdown(label="Humanized Article", value="\n\n\n\n", render=True)
|
1164 |
+
# copy_to_input_btn = gr.Button("Copy to Input for AI Check")
|
1165 |
+
|
1166 |
+
with gr.Tab("History"):
|
1167 |
+
history_chat = gr.Chatbot(label="Generation History", height=1000)
|
1168 |
+
clear_history_btn = gr.Button("Clear History")
|
1169 |
+
clear_history_btn.click(clear_history, outputs=[history, history_chat])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1170 |
"""
|
1171 |
+
# NOTE: REMOVED REFRESH BUTTON
|
1172 |
+
refresh_button = gr.Button("Refresh History")
|
1173 |
+
refresh_button.click(get_history, outputs=history_chat)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1174 |
"""
|
|
|
1175 |
|
1176 |
+
def regenerate_visible(text):
|
1177 |
+
if text:
|
1178 |
+
return gr.update(visible=True)
|
1179 |
+
else:
|
1180 |
+
return gr.update(visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1181 |
|
1182 |
+
def highlight_visible(text):
|
1183 |
+
if text.startswith("Polygraf"):
|
1184 |
+
return gr.update(visible=True)
|
1185 |
+
else:
|
1186 |
+
return gr.update(visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1187 |
|
1188 |
+
def search_visible(toggle):
|
1189 |
+
if toggle:
|
1190 |
+
return gr.update(visible=True)
|
1191 |
+
else:
|
1192 |
+
return gr.update(visible=False)
|
1193 |
+
|
1194 |
+
google_search_check.change(search_visible, inputs=google_search_check, outputs=search_options)
|
1195 |
+
# ai_detector_dropdown.change(highlight_visible, inputs=ai_detector_dropdown, outputs=highlighted_text)
|
1196 |
+
# output_article.change(regenerate_visible, inputs=output_article, outputs=ai_comments)
|
1197 |
+
# ai_comments.change(regenerate_visible, inputs=output_article, outputs=regenerate_btn)
|
1198 |
+
ai_check_btn.click(highlight_visible, inputs=ai_detector_dropdown, outputs=highlighted_text)
|
1199 |
+
|
1200 |
+
# Update the default structure based on the selected format
|
1201 |
+
# e.g. "Plain Text" for certain formats
|
1202 |
+
input_format.change(fn=update_structure, inputs=input_format, outputs=input_structure)
|
1203 |
+
model_dropdown.change(fn=update_temperature, inputs=model_dropdown, outputs=temperature_slider)
|
1204 |
+
report_humanized_btn.click(
|
1205 |
+
save_humanizer_feedback_to_cloud_storage, inputs=[latest_humanizer_data, humanizer_feedback]
|
1206 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1207 |
|
1208 |
+
generate_btn.click(
|
1209 |
+
fn=generate_and_format,
|
1210 |
+
inputs=[
|
1211 |
+
input_role,
|
1212 |
+
input_topic,
|
1213 |
+
input_context,
|
1214 |
+
input_keywords,
|
1215 |
+
input_length,
|
1216 |
+
input_format,
|
1217 |
+
input_writing_style,
|
1218 |
+
input_tone,
|
1219 |
+
input_user_category,
|
1220 |
+
input_depth,
|
1221 |
+
input_structure,
|
1222 |
+
input_references,
|
1223 |
+
input_num_examples,
|
1224 |
+
input_conclusion,
|
1225 |
+
# ai_generator,
|
1226 |
+
# input_api,
|
1227 |
+
google_search_check,
|
1228 |
+
year_from,
|
1229 |
+
month_from,
|
1230 |
+
day_from,
|
1231 |
+
year_to,
|
1232 |
+
month_to,
|
1233 |
+
day_to,
|
1234 |
+
domains_to_include,
|
1235 |
+
include_sites,
|
1236 |
+
exclude_sites,
|
1237 |
+
pdf_file_input,
|
1238 |
+
history,
|
1239 |
+
],
|
1240 |
+
outputs=[output_article, history],
|
1241 |
+
)
|
|
|
|
|
1242 |
|
1243 |
+
regenerate_btn.click(
|
1244 |
+
fn=generate_and_format,
|
1245 |
+
inputs=[
|
1246 |
+
input_role,
|
1247 |
+
input_topic,
|
1248 |
+
input_context,
|
1249 |
+
input_keywords,
|
1250 |
+
input_length,
|
1251 |
+
input_format,
|
1252 |
+
input_writing_style,
|
1253 |
+
input_tone,
|
1254 |
+
input_user_category,
|
1255 |
+
input_depth,
|
1256 |
+
input_structure,
|
1257 |
+
input_references,
|
1258 |
+
input_num_examples,
|
1259 |
+
input_conclusion,
|
1260 |
+
# ai_generator,
|
1261 |
+
# input_api,
|
1262 |
+
google_search_check,
|
1263 |
+
year_from,
|
1264 |
+
month_from,
|
1265 |
+
day_from,
|
1266 |
+
year_to,
|
1267 |
+
month_to,
|
1268 |
+
day_to,
|
1269 |
+
domains_to_include,
|
1270 |
+
pdf_file_input,
|
1271 |
+
history,
|
1272 |
+
output_article,
|
1273 |
+
include_sites,
|
1274 |
+
exclude_sites,
|
1275 |
+
ai_comments,
|
1276 |
+
],
|
1277 |
+
outputs=[output_article, history],
|
1278 |
+
)
|
1279 |
|
1280 |
+
ai_check_btn.click(
|
1281 |
+
fn=ai_check,
|
1282 |
+
inputs=[history, ai_detector_dropdown],
|
1283 |
+
outputs=[ai_check_result, highlighted_text, mc_check_result],
|
1284 |
+
)
|
1285 |
+
|
1286 |
+
humanize_btn.click(
|
1287 |
+
fn=humanize,
|
1288 |
+
inputs=[
|
1289 |
+
model_dropdown,
|
1290 |
+
output_article,
|
1291 |
+
temperature_slider,
|
1292 |
+
repetition_penalty_slider,
|
1293 |
+
top_k_slider,
|
1294 |
+
length_penalty_slider,
|
1295 |
+
history,
|
1296 |
+
],
|
1297 |
+
outputs=[output_article, history, latest_humanizer_data],
|
1298 |
+
)
|
1299 |
|
1300 |
+
generate_btn.click(get_history, inputs=[history], outputs=[history_chat])
|
1301 |
+
regenerate_btn.click(get_history, inputs=[history], outputs=[history_chat])
|
1302 |
+
humanize_btn.click(get_history, inputs=[history], outputs=[history_chat])
|
1303 |
|
1304 |
+
# return demo
|
1305 |
|
1306 |
|
1307 |
if __name__ == "__main__":
|
1308 |
+
# demo = create_interface()
|
1309 |
# demo.queue(
|
1310 |
# max_size=2,
|
1311 |
# default_concurrency_limit=2,
|