File size: 10,327 Bytes
5347681
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import streamlit as st
import random
from langchain_community.chat_models import ChatOpenAI
from langchain.schema import HumanMessage, SystemMessage
from langchain_community.document_loaders import PyPDFLoader, TextLoader, UnstructuredMarkdownLoader, UnstructuredWordDocumentLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.chains import RetrievalQA
import os
from dotenv import load_dotenv
import tempfile
from PIL import Image
import io

# Load environment variables
load_dotenv()

AI71_BASE_URL = "https://api.ai71.ai/v1/"
AI71_API_KEY = os.getenv('AI71_API_KEY')

# Initialize the Falcon model
chat = ChatOpenAI(
    model="tiiuae/falcon-180B-chat",
    api_key=AI71_API_KEY,
    base_url=AI71_BASE_URL,
    streaming=True,
)

# Initialize embeddings
embeddings = HuggingFaceEmbeddings()

def process_documents(uploaded_files):
    documents = []
    for uploaded_file in uploaded_files:
        file_extension = os.path.splitext(uploaded_file.name)[1].lower()
        with tempfile.NamedTemporaryFile(delete=False, suffix=file_extension) as temp_file:
            temp_file.write(uploaded_file.getvalue())
            temp_file_path = temp_file.name

        if file_extension == '.pdf':
            loader = PyPDFLoader(temp_file_path)
        elif file_extension == '.txt':
            loader = TextLoader(temp_file_path)
        elif file_extension == '.md':
            loader = UnstructuredMarkdownLoader(temp_file_path)
        elif file_extension in ['.doc', '.docx']:
            loader = UnstructuredWordDocumentLoader(temp_file_path)
        else:
            st.warning(f"Unsupported file type: {file_extension}")
            continue

        documents.extend(loader.load())
        os.unlink(temp_file_path)

    text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
    texts = text_splitter.split_documents(documents)
    
    vectorstore = FAISS.from_documents(texts, embeddings)
    retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
    
    qa_chain = RetrievalQA.from_chain_type(
        llm=chat,
        chain_type="stuff",
        retriever=retriever,
        return_source_documents=True
    )
    
    return qa_chain

def generate_mnemonic(topic, user_preferences):
    prompt = f"""

    Generate a memorable mnemonic for the topic: {topic}.

    Consider the user's preferences: {user_preferences}.

    The mnemonic should be easy to remember and relate to the topic.

    Also provide a brief explanation of how the mnemonic relates to the topic.

    """
    response = chat.invoke([HumanMessage(content=prompt)])
    return response.content

def generate_quiz_question(mnemonic):
    quiz_prompt = f"""

    Create a quiz question based on the mnemonic: {mnemonic}

    Format your response as follows:

    Question: [Your question here]

    Answer: [Your answer here]

    """
    quiz_response = chat.invoke([HumanMessage(content=quiz_prompt)])
    content = quiz_response.content.strip()
    
    try:
        question_part, answer_part = content.split("Answer:", 1)
        question = question_part.replace("Question:", "").strip()
        answer = answer_part.strip()
    except ValueError:
        question = content
        answer = "Unable to generate a specific answer. Please refer to the mnemonic."
    
    return question, answer

def generate_image_prompt(mnemonic):
    prompt = f"""

    Create a detailed image prompt for Midjourney based on the mnemonic: {mnemonic}

    The image should visually represent the key elements of the mnemonic.

    """
    response = chat.invoke([HumanMessage(content=prompt)])
    return response.content

def main():
    st.set_page_config(page_title="S.H.E.R.L.O.C.K. Mnemonic Generator", page_icon="🧠", layout="wide")

    # Custom CSS
    st.markdown("""

    <style>

    .stApp {

        background-color: #f0f2f6;

    }

    .main .block-container {

        padding-top: 2rem;

    }

    .stButton>button {

        background-color: #4CAF50;

        color: white;

        font-weight: bold;

    }

    .stTextInput>div>div>input {

        background-color: #ffffff;

    }

    </style>

    """, unsafe_allow_html=True)

    st.title("🧠 S.H.E.R.L.O.C.K. Mnemonic Generator")

    # Initialize session state
    if 'generated_mnemonic' not in st.session_state:
        st.session_state.generated_mnemonic = None
    if 'quiz_question' not in st.session_state:
        st.session_state.quiz_question = None
    if 'quiz_answer' not in st.session_state:
        st.session_state.quiz_answer = None
    if 'image_prompt' not in st.session_state:
        st.session_state.image_prompt = None

    # Sidebar
    with st.sidebar:
        st.header("πŸ“š Document Upload")
        uploaded_files = st.file_uploader("Upload documents (optional)", type=["pdf", "txt", "md", "doc", "docx"], accept_multiple_files=True)
        if uploaded_files:
            qa_chain = process_documents(uploaded_files)
            st.success(f"{len(uploaded_files)} document(s) processed successfully!")
        else:
            qa_chain = None

        st.header("🎨 User Preferences")
        user_preferences = st.text_area("Enter your interests or preferences:")

    # Main area
    col1, col2 = st.columns([2, 1])

    with col1:
        st.header("πŸ” Generate Mnemonic")
        topic = st.text_input("Enter the topic for your mnemonic:")
        
        if st.button("Generate Mnemonic"):
            if topic:
                with st.spinner("Generating mnemonic..."):
                    mnemonic = generate_mnemonic(topic, user_preferences)
                st.session_state.generated_mnemonic = mnemonic
                
                with st.spinner("Generating quiz question..."):
                    question, answer = generate_quiz_question(mnemonic)
                st.session_state.quiz_question = question
                st.session_state.quiz_answer = answer

                with st.spinner("Generating image prompt..."):
                    image_prompt = generate_image_prompt(mnemonic)
                st.session_state.image_prompt = image_prompt
            else:
                st.warning("Please enter a topic to generate a mnemonic.")

    with col2:
        if st.session_state.generated_mnemonic:
            st.header("πŸ“ Generated Mnemonic")
            st.write(st.session_state.generated_mnemonic)

    # Quiz section
    if st.session_state.quiz_question:
        st.header("🧠 Mnemonic Quiz")
        st.write(st.session_state.quiz_question)
        user_answer = st.text_input("Your answer:")
        if st.button("Submit Answer"):
            if user_answer.lower() == st.session_state.quiz_answer.lower():
                st.success("πŸŽ‰ Correct! Well done.")
            else:
                st.error(f"❌ Not quite. The correct answer is: {st.session_state.quiz_answer}")

    # Image prompt section
    if st.session_state.image_prompt:
        st.header("πŸ–ΌοΈ Image Prompt")
        st.write(st.session_state.image_prompt)
        st.info("You can use this prompt with Midjourney or other image generation tools to create a visual representation of your mnemonic.")

    # Document Q&A section
    if qa_chain:
        st.header("πŸ“š Document Q&A")
        user_question = st.text_input("Ask a question about the uploaded document(s):")
        if st.button("Get Answer"):
            with st.spinner("Searching for the answer..."):
                result = qa_chain({"query": user_question})
                st.subheader("Answer:")
                st.write(result["result"])
                st.subheader("Sources:")
                for source in result["source_documents"]:
                    st.write(source.page_content)

    # Mnemonic visualization
    if st.session_state.generated_mnemonic:
        st.header("🎨 Mnemonic Visualization")
        visualization_type = st.selectbox("Choose visualization type:", ["Word Cloud", "Mind Map"])
        if st.button("Generate Visualization"):
            with st.spinner("Generating visualization..."):
                visualization_prompt = f"""

                Create a detailed description of a {visualization_type} based on the mnemonic:

                {st.session_state.generated_mnemonic}

                Describe the layout, key elements, and their relationships.

                """
                visualization_description = chat.invoke([HumanMessage(content=visualization_prompt)]).content
                st.write(visualization_description)
                st.info("You can use this description to create a visual representation of your mnemonic using tools like Canva or Mindmeister.")

    # Export options
    if st.session_state.generated_mnemonic:
        st.header("πŸ“€ Export Options")
        export_format = st.selectbox("Choose export format:", ["Text", "PDF", "Markdown"])
        if st.button("Export Mnemonic"):
            export_content = f"""

            Topic: {topic}

            

            Mnemonic:

            {st.session_state.generated_mnemonic}

            

            Quiz Question:

            {st.session_state.quiz_question}

            

            Quiz Answer:

            {st.session_state.quiz_answer}

            

            Image Prompt:

            {st.session_state.image_prompt}

            """
            
            if export_format == "Text":
                st.download_button("Download Text", export_content, file_name="mnemonic_export.txt")
            elif export_format == "PDF":
                # You'd need to implement PDF generation here, for example using reportlab
                st.warning("PDF export not implemented in this example.")
            elif export_format == "Markdown":
                st.download_button("Download Markdown", export_content, file_name="mnemonic_export.md")

    # Footer
    st.sidebar.markdown("---")
    st.sidebar.markdown("Powered by Falcon-180B and Streamlit")

if __name__ == "__main__":
    main()