brianjking commited on
Commit
6b9ee13
1 Parent(s): da11e22

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from llama_index import (
4
+ ServiceContext,
5
+ SimpleDirectoryReader,
6
+ VectorStoreIndex,
7
+ )
8
+
9
+ # Import OpenAI only once, avoiding naming conflict
10
+ from openai import OpenAI as OpenAIClient
11
+
12
+ client = OpenAIClient(api_key=os.getenv("OPENAI_API_KEY"))
13
+
14
+ # Define Streamlit layout and interaction
15
+ st.title("Grounded Generations")
16
+
17
+ # Upload PDF
18
+ uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
19
+
20
+ @st.cache_resource(show_spinner=False)
21
+ def load_data(uploaded_file):
22
+ with st.spinner('Indexing document...'):
23
+ # Save the uploaded file temporarily
24
+ with open("temp.pdf", "wb") as f:
25
+ f.write(uploaded_file.read())
26
+ # Read and index documents using SimpleDirectoryReader
27
+ reader = SimpleDirectoryReader(input_dir="./", recursive=False)
28
+ docs = reader.load_data()
29
+ # The model configuration should be moved to where you actually call the OpenAI API
30
+ service_context = ServiceContext.from_defaults(
31
+ system_prompt="You are an AI assistant that uses context from a PDF to assist the user in generating text."
32
+ )
33
+ index = VectorStoreIndex.from_documents(docs, service_context=service_context)
34
+ return index
35
+
36
+ # Placeholder for document indexing
37
+ if uploaded_file is not None:
38
+ index = load_data(uploaded_file)
39
+
40
+ # Take user query input
41
+ user_query = st.text_input("Search for the products/info you want to use to ground your generated text content:")
42
+
43
+ # Initialize session_state for retrieved_text if not already present
44
+ if 'retrieved_text' not in st.session_state:
45
+ st.session_state['retrieved_text'] = ''
46
+
47
+ # Search and display retrieved text
48
+ if st.button("Retrieve"):
49
+ with st.spinner('Retrieving text...'):
50
+ # Use VectorStoreIndex to search
51
+ query_engine = index.as_query_engine(similarity_top_k=3)
52
+ st.session_state['retrieved_text'] = query_engine.query(user_query)
53
+ st.write(f"Retrieved Text: {st.session_state['retrieved_text']}")
54
+
55
+ # Select content type
56
+ content_type = st.selectbox("Select content type:", ["Blog", "Tweet"])
57
+
58
+ # Generate text based on retrieved text and selected content type
59
+ if st.button("Generate") and content_type:
60
+ with st.spinner('Generating text...'):
61
+ try:
62
+ 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']}"
63
+ response = client.chat.completions.create(model="gpt-3.5-turbo-16k",
64
+ messages=[
65
+ {"role": "system", "content": "You are a helpful assistant."},
66
+ {"role": "user", "content": prompt}
67
+ ])
68
+ generated_text = response.choices[0].message.content
69
+ st.write(f"Generated Text: {generated_text}")
70
+ except Exception as e:
71
+ st.write(f"An error occurred: {e}")