File size: 4,478 Bytes
e29cfc3
ff66db5
 
 
 
 
 
 
 
 
 
e29cfc3
 
ff66db5
 
e29cfc3
ff66db5
e29cfc3
 
ff66db5
e29cfc3
f91d645
ff66db5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303a408
f91d645
e29cfc3
 
ff66db5
 
 
e29cfc3
f91d645
e29cfc3
 
 
 
 
 
720b01b
 
e29cfc3
303a408
f91d645
e29cfc3
 
 
 
 
 
 
 
 
303a408
e29cfc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff66db5
 
e29cfc3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import streamlit as st
from langchain.llms import CTransformers
from langchain.chains import LLMChain
from langchain import PromptTemplate
import os
from docx import Document
from docx.shared import Inches
import io
from PIL import Image
import requests

# Loading the model
def load_llm(max_tokens, prompt_template, temperature):
    # Load the locally downloaded model here
    llm = CTransformers(
        model="llama-2-7b-chat.ggmlv3.q8_0.bin",
        model_type="llama",
        max_new_tokens=max_tokens,
        temperature=temperature,
    )

    llm_chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template(prompt_template))
    return llm_chain

def get_src_original_url(query):
    url = 'https://api.pexels.com/v1/search'
    headers = {
        'Authorization': "iMn2jjJXgPCqmalZsrDxYA5WcLSyt1FgopsBxY4M8rUxRc4POC83rsR3",
    }

    params = {
        'query': query,
        'per_page': 1,
    }

    response = requests.get(url, headers=headers, params=params)

    # Check if the request was successful (status code 200)
    if response.status_code == 200:
        data = response.json()
        photos = data.get('photos', [])
        if photos:
            src_original_url = photos[0]['src']['original']
            return src_original_url
        else:
            st.write("No photos found for the given query.")
    else:
        st.write(f"Error: {response.status_code}, {response.text}")

    return None

def create_word_docx(user_input, paragraph, image_input):
    # Create a new Word document
    doc = Document()

    # Add the user input to the document
    doc.add_heading(user_input, level=1)
    doc.add_paragraph(paragraph)

    # Add the image to the document
    doc.add_heading('Image Input', level=1)
    image_stream = io.BytesIO()
    image_input.save(image_stream, format='PNG')
    image_stream.seek(0)
    doc.add_picture(image_stream, width=Inches(4))  # Adjust the width as needed

    return doc

st.set_page_config(layout="wide")

def main():
    st.title("GeniusWords: Unleash Your Imagination")

    example_prompt_template = 'You are a digital marketing and SEO expert and your task is to write an article on the given topic: {user_input}.'
    prompt_template = st.text_area("Please enter the prompt template:", value=example_prompt_template)
    max_tokens = st.number_input("Please enter the maximum tokens:", min_value=1, value=800)
    temperature = st.slider("Select the temperature:", min_value=0.1, max_value=1.0, value=0.7, step=0.1)
    user_input = st.text_input("Please enter the idea/topic for the article you want to generate!")
    image_input = st.text_input("Please enter the topic for the image you want to fetch!")

    if st.button("Generate Article"):
        if len(user_input) > 0 and len(image_input) > 0 and len(prompt_template) > 0:
            col1, col2, col3 = st.columns([1, 2, 1])
            with col1:
                st.subheader("Generated Content")
                st.write("Topic of the article is: " + user_input)
                st.write("Image of the article is: " + image_input)

                llm_call = load_llm(max_tokens=max_tokens, prompt_template=prompt_template, temperature=temperature)
                result = llm_call(user_input)
                if len(result) > 0:
                    st.info("Your Content has been generated successfully!")
                    st.write(result)
                else:
                    st.error("Your article couldn't be generated!")

            with col2:
                st.subheader("Fetched Image")
                image_url = get_src_original_url(image_input)
                st.image(image_url)

            with col3:
                st.subheader("Final Content to Download")
                image_response = requests.get(image_url)
                img = Image.open(io.BytesIO(image_response.content))
                doc = create_word_docx(user_input, result['text'], img)

                # Save the Word document to a BytesIO buffer
                doc_buffer = io.BytesIO()
                doc.save(doc_buffer)
                doc_buffer.seek(0)

                # Prepare the download link
                st.download_button(
                    label='Download Word Document',
                    data=doc_buffer,
                    file_name='document.docx',
                    mime='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
                )

if __name__ == "__main__":
    main()