Sakil commited on
Commit
e29cfc3
·
1 Parent(s): 3189e5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -58
app.py CHANGED
@@ -1,7 +1,7 @@
 
1
  from langchain.llms import CTransformers
2
  from langchain.chains import LLMChain
3
  from langchain import PromptTemplate
4
- import streamlit as st
5
  import os
6
  from docx import Document
7
  from docx.shared import Inches
@@ -9,21 +9,17 @@ import io
9
  from PIL import Image
10
  import requests
11
 
12
- #Loading the model
13
- def load_llm(max_tokens, prompt_template):
14
  # Load the locally downloaded model here
15
  llm = CTransformers(
16
- model = "llama-2-7b-chat.ggmlv3.q8_0.bin",
17
  model_type="llama",
18
- max_new_tokens = max_tokens,
19
- temperature = 0.7
20
  )
21
-
22
- llm_chain = LLMChain(
23
- llm=llm,
24
- prompt=PromptTemplate.from_template(prompt_template)
25
- )
26
- print(llm_chain)
27
  return llm_chain
28
 
29
  def get_src_original_url(query):
@@ -75,54 +71,51 @@ st.set_page_config(layout="wide")
75
  def main():
76
  st.title("GeniusWords: Unleash Your Imagination")
77
 
 
 
 
78
  user_input = st.text_input("Please enter the idea/topic for the article you want to generate!")
79
-
80
  image_input = st.text_input("Please enter the topic for the image you want to fetch!")
81
 
82
- if len(user_input) > 0 and len(image_input) > 0:
83
-
84
- col1, col2, col3 = st.columns([1,2,1])
85
-
86
- with col1:
87
- st.subheader("Generated Content")
88
- st.write("Topic of the article is: " + user_input)
89
- st.write("Image of the article is: " + image_input)
90
- prompt_template = """You are a digital marketing and SEO expert and your task is to write article so write an article on the given topic: {user_input}. The article must be under 800 words. The article should be be lengthy.
91
- """
92
- llm_call = load_llm(max_tokens=800, prompt_template=prompt_template)
93
- print(llm_call)
94
- result = llm_call(user_input)
95
- if len(result) > 0:
96
- st.info("Your article has been been generated successfully!")
97
- st.write(result)
98
- else:
99
- st.error("Your article couldn't be generated!")
100
-
101
- with col2:
102
- st.subheader("Fetched Image")
103
- image_url = get_src_original_url(image_input)
104
- st.image(image_url)
105
-
106
- with col3:
107
- st.subheader("Final Article to Download")
108
- #image_input = "temp_image.jpg"
109
- image_response=requests.get(image_url)
110
- img=Image.open(io.BytesIO(image_response.content))
111
- doc = create_word_docx(user_input, result['text'], img)
112
-
113
- # Save the Word document to a BytesIO buffer
114
- doc_buffer = io.BytesIO()
115
- doc.save(doc_buffer)
116
- doc_buffer.seek(0)
117
-
118
- # Prepare the download link
119
- st.download_button(
120
- label='Download Word Document',
121
- data=doc_buffer,
122
- file_name='document.docx',
123
- mime='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
124
- )
125
-
126
 
127
  if __name__ == "__main__":
128
- main()
 
1
+ import streamlit as st
2
  from langchain.llms import CTransformers
3
  from langchain.chains import LLMChain
4
  from langchain import PromptTemplate
 
5
  import os
6
  from docx import Document
7
  from docx.shared import Inches
 
9
  from PIL import Image
10
  import requests
11
 
12
+ # Loading the model
13
+ def load_llm(max_tokens, prompt_template, temperature):
14
  # Load the locally downloaded model here
15
  llm = CTransformers(
16
+ model="llama-2-7b-chat.ggmlv3.q8_0.bin",
17
  model_type="llama",
18
+ max_new_tokens=max_tokens,
19
+ temperature=temperature,
20
  )
21
+
22
+ llm_chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template(prompt_template))
 
 
 
 
23
  return llm_chain
24
 
25
  def get_src_original_url(query):
 
71
  def main():
72
  st.title("GeniusWords: Unleash Your Imagination")
73
 
74
+ prompt_template = st.text_area("Please enter the prompt template:")
75
+ max_tokens = st.number_input("Please enter the maximum tokens:", min_value=1, value=800)
76
+ temperature = st.slider("Select the temperature:", min_value=0.1, max_value=1.0, value=0.7, step=0.1)
77
  user_input = st.text_input("Please enter the idea/topic for the article you want to generate!")
 
78
  image_input = st.text_input("Please enter the topic for the image you want to fetch!")
79
 
80
+ if st.button("Generate Article"):
81
+ if len(user_input) > 0 and len(image_input) > 0 and len(prompt_template) > 0:
82
+ col1, col2, col3 = st.columns([1, 2, 1])
83
+ with col1:
84
+ st.subheader("Generated Content")
85
+ st.write("Topic of the article is: " + user_input)
86
+ st.write("Image of the article is: " + image_input)
87
+
88
+ llm_call = load_llm(max_tokens=max_tokens, prompt_template=prompt_template, temperature=temperature)
89
+ result = llm_call(user_input)
90
+ if len(result) > 0:
91
+ st.info("Your article has been generated successfully!")
92
+ st.write(result)
93
+ else:
94
+ st.error("Your article couldn't be generated!")
95
+
96
+ with col2:
97
+ st.subheader("Fetched Image")
98
+ image_url = get_src_original_url(image_input)
99
+ st.image(image_url)
100
+
101
+ with col3:
102
+ st.subheader("Final Article to Download")
103
+ image_response = requests.get(image_url)
104
+ img = Image.open(io.BytesIO(image_response.content))
105
+ doc = create_word_docx(user_input, result['text'], img)
106
+
107
+ # Save the Word document to a BytesIO buffer
108
+ doc_buffer = io.BytesIO()
109
+ doc.save(doc_buffer)
110
+ doc_buffer.seek(0)
111
+
112
+ # Prepare the download link
113
+ st.download_button(
114
+ label='Download Word Document',
115
+ data=doc_buffer,
116
+ file_name='document.docx',
117
+ mime='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
118
+ )
 
 
 
 
 
119
 
120
  if __name__ == "__main__":
121
+ main()