File size: 3,080 Bytes
8bcc00e
 
 
 
 
 
 
6fc8fb7
9e96cd4
 
 
 
8bcc00e
 
57025a9
8bcc00e
 
 
 
 
 
 
 
 
 
 
 
 
9e96cd4
8bcc00e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
477c74c
8bcc00e
9e96cd4
8bcc00e
 
9e96cd4
8bcc00e
 
 
 
 
9e96cd4
6fc8fb7
9e96cd4
 
 
 
6fc8fb7
8bcc00e
 
9e96cd4
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
import streamlit as st
import os
from llama_index import (
    ServiceContext,
    SimpleDirectoryReader,
    VectorStoreIndex,
)

# Import OpenAI only once, avoiding naming conflict
from openai import OpenAI as OpenAIClient

client = OpenAIClient(api_key=os.getenv("OPENAI_API_KEY"))

# Define Streamlit layout and interaction
st.title("Grounded Generations")

# Upload PDF
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")

@st.cache_resource(show_spinner=False)
def load_data(uploaded_file):
    with st.spinner('Indexing document...'):
        # Save the uploaded file temporarily
        with open("temp.pdf", "wb") as f:
            f.write(uploaded_file.read())
        # Read and index documents using SimpleDirectoryReader
        reader = SimpleDirectoryReader(input_dir="./", recursive=False)
        docs = reader.load_data()
        # The model configuration should be moved to where you actually call the OpenAI API
        service_context = ServiceContext.from_defaults(
            system_prompt="You are an AI assistant that uses context from a PDF to assist the user in generating text."
        )
        index = VectorStoreIndex.from_documents(docs, service_context=service_context)
    return index

# Placeholder for document indexing
if uploaded_file is not None:
    index = load_data(uploaded_file)

# Take user query input
user_query = st.text_input("Search for the products/info you want to use to ground your generated text content:")

# Initialize session_state for retrieved_text if not already present
if 'retrieved_text' not in st.session_state:
    st.session_state['retrieved_text'] = ''

# Search and display retrieved text
if st.button("Retrieve"):
    with st.spinner('Retrieving text...'):
        # Use VectorStoreIndex to search
        query_engine = index.as_query_engine(similarity_top_k=3) 
        st.session_state['retrieved_text'] = query_engine.query(user_query)
        st.write(f"Retrieved Text: {st.session_state['retrieved_text']}")

# Select content type
content_type = st.selectbox("Select content type:", ["Blog", "Tweet"])

# Generate text based on retrieved text and selected content type
if st.button("Generate") and content_type:
    with st.spinner('Generating text...'):
        try:
            prompt = f"Write a blog about 500 words in length using {st.session_state['retrieved_text']}" if content_type == "Blog" else f"Compose a tweet using {st.session_state['retrieved_text']}"
            response = client.chat.completions.create(model="gpt-3.5-turbo-16k",
                                                      messages=[
                                                          {"role": "system", "content": "You are a helpful assistant."},
                                                          {"role": "user", "content": prompt}
                                                      ])
            generated_text = response.choices[0].message.content
            st.write(f"Generated Text: {generated_text}")
        except Exception as e:
            st.write(f"An error occurred: {e}")