Spaces:
Runtime error
Runtime error
app code added
Browse files
app.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 8 |
+
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):
|
| 30 |
+
url = 'https://api.pexels.com/v1/search'
|
| 31 |
+
headers = {
|
| 32 |
+
'Authorization': "iMn2jjJXgPCqmalZsrDxYA5WcLSyt1FgopsBxY4M8rUxRc4POC83rsR3",
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
params = {
|
| 36 |
+
'query': query,
|
| 37 |
+
'per_page': 1,
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
response = requests.get(url, headers=headers, params=params)
|
| 41 |
+
|
| 42 |
+
# Check if the request was successful (status code 200)
|
| 43 |
+
if response.status_code == 200:
|
| 44 |
+
data = response.json()
|
| 45 |
+
photos = data.get('photos', [])
|
| 46 |
+
if photos:
|
| 47 |
+
src_original_url = photos[0]['src']['original']
|
| 48 |
+
return src_original_url
|
| 49 |
+
else:
|
| 50 |
+
st.write("No photos found for the given query.")
|
| 51 |
+
else:
|
| 52 |
+
st.write(f"Error: {response.status_code}, {response.text}")
|
| 53 |
+
|
| 54 |
+
return None
|
| 55 |
+
|
| 56 |
+
def create_word_docx(user_input, paragraph, image_input):
|
| 57 |
+
# Create a new Word document
|
| 58 |
+
doc = Document()
|
| 59 |
+
|
| 60 |
+
# Add the user input to the document
|
| 61 |
+
doc.add_heading(user_input, level=1)
|
| 62 |
+
doc.add_paragraph(paragraph)
|
| 63 |
+
|
| 64 |
+
# Add the image to the document
|
| 65 |
+
doc.add_heading('Image Input', level=1)
|
| 66 |
+
image_stream = io.BytesIO()
|
| 67 |
+
image_input.save(image_stream, format='PNG')
|
| 68 |
+
image_stream.seek(0)
|
| 69 |
+
doc.add_picture(image_stream, width=Inches(4)) # Adjust the width as needed
|
| 70 |
+
|
| 71 |
+
return doc
|
| 72 |
+
|
| 73 |
+
st.set_page_config(layout="wide")
|
| 74 |
+
|
| 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()
|