Skip to content

Commit

Permalink
fix(*): update create post with twitter
Browse files Browse the repository at this point in the history
  • Loading branch information
HoseaCodes committed Jun 4, 2024
1 parent 534a137 commit ff83b5f
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 88 deletions.
16 changes: 12 additions & 4 deletions src/Components/OpenAI/AITemplate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ import Button from "react-bootstrap/Button";
import Modal from "react-bootstrap/Modal";

function AITemplate(props) {
const { articleInput, showAITemplate, setShowAITemplate, setLinkedinResult } =
props;
const {
articleInput,
showAITemplate,
setShowAITemplate,
setLinkedinResult,
setTwitterResult,
} = props;
const [result, setResult] = useState("");
const [input, setInput] = useState("");
const [option, setOption] = useState({});
Expand All @@ -23,8 +28,11 @@ function AITemplate(props) {
messages: [{ role: "assistant", content: input }],
model: "gpt-3.5-turbo",
});
setResult(response.choices[0].message.content);
setLinkedinResult(response.choices[0].message.content);
if (option.includes("LinkedIn")) {
setLinkedinResult(response.choices[0].message.content);
} else {
setTwitterResult(response.choices[0].message.content);
}
} catch (error) {
console.log(error);
alert("Error in AI", error);
Expand Down
14 changes: 13 additions & 1 deletion src/Components/OpenAI/SelectionOptions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ function OptionSelection(props) {
presence_penalty: 0.0,
},
},
{
name: "Generate Tweet",
description: `${templates.twitter}: ${articleInput} with tone: ${templates.tone}`,
option: {
model: "gpt-3.5-turbo",
temperature: 0.7,
max_tokens: 64,
top_p: 1.0,
frequency_penalty: 0.0,
presence_penalty: 0.0,
},
},
// {
// name: "Summarize for a 2nd grader",
// id: "summary",
Expand Down Expand Up @@ -202,7 +214,7 @@ function OptionSelection(props) {
// },
];
const handleClick = (item) => {
selectOption(item.option);
selectOption(item.name);
setInput(item.description);
};

Expand Down
16 changes: 14 additions & 2 deletions src/Constants/ai.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,20 @@ const templates = {
• Wrap up with a final recommendation and a light-hearted sign-off.
• Include a small humorous or casual element to leave the reader with a positive impression.
"
`,
twitter: `
You will act as an experienced Twitter Social Media Expert and Business Analyst.
You will write engaging an engaging Twitter post that provides value or insights on a topic.
Write the post in English.
- You approach serious topics related to business, and productivity in a professional way to make them more accessible to the general public.
- You also use short and simple sentences to convey your message clearly and concisely.
- Overall, your tone is optimistic and encouraging, with a touch of humor.
- Use a question at the end of your text to engage your audience and elicit their reaction.
- You consistently encourage interaction by inviting your audience to comment and share their own experiences.
- Include 2-3 relevant hashtags at the end of the post.
- Now, I would like you to write a Twitter post in my style on the following topic:
`,
};

export default templates;
export default templates;
212 changes: 131 additions & 81 deletions src/Pages/Articles/CreateArticle.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ function CreateArticle() {
scheduled: false,
linkedin: false,
linkedinContent: "",
tweet: false,
tweetContent: "",
scheduledDate: todaysDate,
images: {
secure_url: "",
Expand All @@ -62,7 +64,10 @@ function CreateArticle() {
const [linkedinResult, setLinkedinResult] = useState("");
const [linkedinAccessToken, setLinkedinAccessToken] = useState("");
const [keywords, setKeywords] = useState([]);
const [keywords2, setKeywords2] = useState([{ word: "", count: 0, percentage: "" }]);
const [keywords2, setKeywords2] = useState([
{ word: "", count: 0, percentage: "" },
]);
const [twitterResult, setTwitterResult] = useState("");

useEffect(() => {
const urlParams = new URLSearchParams(window.location.search);
Expand Down Expand Up @@ -114,13 +119,13 @@ function CreateArticle() {
let kwords = extractKeywords2(article.markdown);
kwords = kwords.splice(0, 5);
setKeywords2(kwords);
console.log(kwords)
console.log(kwords);
kwords.forEach((keyword) => {
console.log(
`${keyword.word}: Count - ${keyword.count}, Percentage - ${keyword.percentage}`
);
});
}, [article.markdown]);
}, [article.markdown]);

const styleUpload = {
display: images ? "block" : "none",
Expand Down Expand Up @@ -276,84 +281,82 @@ function CreateArticle() {
}
};

function extractKeywords2(text) {
// Define words to ignore (definite articles, determiners, and conjunctions)
const ignoreWords = [
"a",
"an",
"the",
"and",
"but",
"or",
"for",
"nor",
"so",
"if",
"as",
"at",
"by",
"in",
"of",
"on",
"to",
"with",
"from",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",

];

// Step 1: Normalize the text
text = text.toLowerCase().replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, "");
text = text.replace(/[^\w\s]/g, '');

// Step 2: Split the text into words
const words = text.split(/\s+/);

// Step 3: Filter out the words to ignore
const filteredWords = words.filter((word) => !ignoreWords.includes(word));

// Step 4: Count the frequency of each word
const wordCount = {};
filteredWords.forEach((word) => {
if (wordCount[word]) {
wordCount[word]++;
} else {
wordCount[word] = 1;
}
});

// Step 5: Calculate the total number of valid words
const totalWords = filteredWords.length;

// Step 6: Calculate the percentage of each word
const wordStats = [];
for (let word in wordCount) {
const count = wordCount[word];
const percentage = ((count / totalWords) * 100).toFixed(2);
const capitalizedWord = word.charAt(0).toUpperCase() + word.slice(1);
wordStats.push({
word: capitalizedWord,
count,
percentage: `${percentage}%`,
function extractKeywords2(text) {
// Define words to ignore (definite articles, determiners, and conjunctions)
const ignoreWords = [
"a",
"an",
"the",
"and",
"but",
"or",
"for",
"nor",
"so",
"if",
"as",
"at",
"by",
"in",
"of",
"on",
"to",
"with",
"from",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
];

// Step 1: Normalize the text
text = text.toLowerCase().replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, "");
text = text.replace(/[^\w\s]/g, "");

// Step 2: Split the text into words
const words = text.split(/\s+/);

// Step 3: Filter out the words to ignore
const filteredWords = words.filter((word) => !ignoreWords.includes(word));

// Step 4: Count the frequency of each word
const wordCount = {};
filteredWords.forEach((word) => {
if (wordCount[word]) {
wordCount[word]++;
} else {
wordCount[word] = 1;
}
});
}

// Step 7: Sort words by frequency
wordStats.sort((a, b) => b.count - a.count);
// Step 5: Calculate the total number of valid words
const totalWords = filteredWords.length;

// Step 6: Calculate the percentage of each word
const wordStats = [];
for (let word in wordCount) {
const count = wordCount[word];
const percentage = ((count / totalWords) * 100).toFixed(2);
const capitalizedWord = word.charAt(0).toUpperCase() + word.slice(1);
wordStats.push({
word: capitalizedWord,
count,
percentage: `${percentage}%`,
});
}

// Step 8: Return the formatted result
return wordStats;
}
// Step 7: Sort words by frequency
wordStats.sort((a, b) => b.count - a.count);

// Step 8: Return the formatted result
return wordStats;
}

console.log({ article });
return (
Expand Down Expand Up @@ -480,7 +483,12 @@ function extractKeywords2(text) {
</div>
</div>
<div
style={{ display: "flex", width: "100%", flexDirection: "row-reverse", width: "100%"}}
style={{
display: "flex",
width: "100%",
flexDirection: "row-reverse",
width: "100%",
}}
className={onEdit ? "d-none" : `col-md-6`}
>
<div className="d-flex flex-column h-90 w-100 justify-content-between">
Expand Down Expand Up @@ -519,6 +527,23 @@ function extractKeywords2(text) {
aria-label="Checkbox for following text input"
/>
</div>
<div className="controls d-flex flex-row align-items-center">
<label
for="markdown"
className="control-label"
requiredField
>
Publish To Twitter
<span className="pr-1 asteriskField">*</span>
</label>
<input
className="bg-transparent"
type="checkbox"
name="twitter"
onChange={(e) => handlePublish(e)}
aria-label="Checkbox for following text input"
/>
</div>
</div>
<div className="d-flex flex-column h-90 w-100 justify-content-between">
<div className="controls d-flex flex-column ">
Expand Down Expand Up @@ -629,7 +654,7 @@ function extractKeywords2(text) {
for="p_name"
className="control-label requiredField"
>
LinkedIn Content
Twitter Content
<span className="asteriskField">*</span>{" "}
</label>{" "}
&nbsp;&nbsp;
Expand All @@ -638,9 +663,34 @@ function extractKeywords2(text) {
articleInput={article.markdown}
showAITemplate={showAITemplate}
setShowAITemplate={setShowAITemplate}
setTwitterResult={setTwitterResult}
setLinkedinResult={setLinkedinResult}
/>
)}
<div className="controls ">
<br />
<textarea
className="mb bg-transparent"
name="tweetContent"
required
value={article.tweetContent || twitterResult}
onChange={handleChangeInput}
style={{ width: "100%" }}
rows="5"
cols="50"
></textarea>
</div>
</div>
<div id="div_description" className=" required">
<br />
<label
for="p_name"
className="control-label requiredField"
>
LinkedIn Content
<span className="asteriskField">*</span>{" "}
</label>{" "}
&nbsp;&nbsp;
<div className="controls ">
<br />
<textarea
Expand Down Expand Up @@ -858,10 +908,10 @@ function extractKeywords2(text) {
article={article}
/>
</div>
<AITemplate
{/* <AITemplate
showAITemplate={showAITemplate}
setShowAITemplate={setShowAITemplate}
/>
/> */}
</div>
</div>
</div>
Expand Down

0 comments on commit ff83b5f

Please sign in to comment.