Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
| 17 |
model_type="llama",
|
| 18 |
-
max_new_tokens
|
| 19 |
-
temperature
|
| 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
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
st.
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 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()
|