AamirAli123
commited on
Commit
•
e31d3f6
1
Parent(s):
2db4b83
Upload project files
Browse files- app.py +34 -0
- module/generator.py +86 -0
- prompt/blog_or_essay_prompt.txt +10 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
main module blog generator.
|
3 |
+
"""
|
4 |
+
import gradio as gr
|
5 |
+
from module.generator import (generate_blog)
|
6 |
+
###################### Front-end ########################
|
7 |
+
with gr.Blocks(theme = gr.themes.Monochrome()) as demo:
|
8 |
+
gr.Markdown("""
|
9 |
+
<h1><center>Writer.ai</center></h1>
|
10 |
+
<h3><center>Blog and essay writer ai tool</center></h3>
|
11 |
+
""")
|
12 |
+
with gr.Column():
|
13 |
+
output_block = gr.Markdown(line_breaks = True)
|
14 |
+
with gr.Row():
|
15 |
+
topic = gr.Text(label = "Topic",
|
16 |
+
placeholder = "essay or blog topic")
|
17 |
+
content_type = gr.Text(label = "Type",
|
18 |
+
placeholder = "essay, blog post")
|
19 |
+
tone = gr.Text(label = "Tone",
|
20 |
+
placeholder = "soft, professional")
|
21 |
+
link = gr.Text(label = "link",
|
22 |
+
placeholder = "links with comma seperated")
|
23 |
+
length = gr.Text(label = "Length",
|
24 |
+
placeholder = "length of generated contents")
|
25 |
+
with gr.Row():
|
26 |
+
stop_button = gr.Button("Stop")
|
27 |
+
resume_button = gr.Button("Resume")
|
28 |
+
start_button = gr.Button("Start")
|
29 |
+
start_button.click(fn = generate_blog, inputs = [topic,
|
30 |
+
content_type, link,
|
31 |
+
tone, length],
|
32 |
+
outputs = output_block)
|
33 |
+
demo.queue(max_size = 1)
|
34 |
+
demo.launch(debug = True)
|
module/generator.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
noun generator module will take the string and return the nouns list in string.
|
3 |
+
"""
|
4 |
+
import os
|
5 |
+
import openai
|
6 |
+
from dotenv import load_dotenv, find_dotenv
|
7 |
+
_ = load_dotenv(find_dotenv())
|
8 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
9 |
+
def make_prompt()-> str:
|
10 |
+
"""
|
11 |
+
make_prompt take code as input and retirn the prompt with the given
|
12 |
+
pargraph.
|
13 |
+
|
14 |
+
Parameters
|
15 |
+
----------
|
16 |
+
paragraph: str
|
17 |
+
string text to find the nouns.
|
18 |
+
action: str
|
19 |
+
type of action.
|
20 |
+
Return
|
21 |
+
------
|
22 |
+
prompt: str
|
23 |
+
prompt to find the nouns from given paragraph.
|
24 |
+
"""
|
25 |
+
file_path = "./prompt/blog_or_essay_prompt.txt"
|
26 |
+
with open(file_path, "r", encoding = "utf8") as file:
|
27 |
+
prompt = file.read()
|
28 |
+
return prompt
|
29 |
+
def generate_blog(topic = " ",
|
30 |
+
content_type = " ", link = " ", tone = " ",
|
31 |
+
length = 500):
|
32 |
+
"""
|
33 |
+
code_debug method take topic type link tone length as input and return the output
|
34 |
+
according to the prompt.
|
35 |
+
|
36 |
+
Parameters
|
37 |
+
----------
|
38 |
+
topic: str
|
39 |
+
topic of essay or blog.
|
40 |
+
type: str
|
41 |
+
essay or blog.
|
42 |
+
link: str
|
43 |
+
link of user sources.
|
44 |
+
tone: str
|
45 |
+
essay or blog tone.
|
46 |
+
Return
|
47 |
+
------
|
48 |
+
return generated blog or essay.
|
49 |
+
"""
|
50 |
+
full_text = ""
|
51 |
+
length = str(length)
|
52 |
+
prompt = make_prompt()
|
53 |
+
prompt = prompt.format(TOPIC = topic, WORDS = length,
|
54 |
+
TYPE = content_type,
|
55 |
+
LINKS = link
|
56 |
+
)
|
57 |
+
tone_prompt = f"tone should be {tone}"
|
58 |
+
messages=[
|
59 |
+
{
|
60 |
+
"role": "system",
|
61 |
+
"content": prompt
|
62 |
+
},
|
63 |
+
{
|
64 |
+
"role": "user",
|
65 |
+
"content": tone_prompt
|
66 |
+
}
|
67 |
+
]
|
68 |
+
response = openai.ChatCompletion.create(
|
69 |
+
model="gpt-3.5-turbo",
|
70 |
+
messages = messages,
|
71 |
+
temperature = 1,
|
72 |
+
top_p = 1,
|
73 |
+
frequency_penalty = 0,
|
74 |
+
presence_penalty = 0,
|
75 |
+
stream = True,
|
76 |
+
stop = None
|
77 |
+
)
|
78 |
+
try:
|
79 |
+
for chunk in response:
|
80 |
+
chunk_message = chunk['choices'][0]['delta'].get("content")
|
81 |
+
full_text = full_text + chunk_message
|
82 |
+
yield full_text
|
83 |
+
except Exception as error:
|
84 |
+
print("OPenAI reponse (streaming) error" + str(error))
|
85 |
+
return 503
|
86 |
+
|
prompt/blog_or_essay_prompt.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Write a {TYPE} in markdown must include a table of contents in circle bullet of at least {WORDS} words on {TOPIC}.
|
2 |
+
Each section must contain 1-2 paragraphs.
|
3 |
+
Give references in markdown format with numbred bullet.
|
4 |
+
Add our links heading in last and add {LINKS} as given below.
|
5 |
+
Examples
|
6 |
+
[Facebook](https://facebook.com)
|
7 |
+
[Twitter](https://Twitter.com)
|
8 |
+
[Linkedin](https://Linkedin.com)
|
9 |
+
[Youtube](https://Youtube.com)
|
10 |
+
[other]((https://other.com))
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
python-dotenv
|