Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,10 +5,11 @@ from llama_index import (
|
|
| 5 |
SimpleDirectoryReader,
|
| 6 |
VectorStoreIndex,
|
| 7 |
)
|
| 8 |
-
from llama_index.llms import OpenAI
|
| 9 |
-
from openai import OpenAI
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Define Streamlit layout and interaction
|
| 14 |
st.title("Grounded Generations")
|
|
@@ -25,11 +26,8 @@ def load_data(uploaded_file):
|
|
| 25 |
# Read and index documents using SimpleDirectoryReader
|
| 26 |
reader = SimpleDirectoryReader(input_dir="./", recursive=False)
|
| 27 |
docs = reader.load_data()
|
|
|
|
| 28 |
service_context = ServiceContext.from_defaults(
|
| 29 |
-
llm=OpenAI(
|
| 30 |
-
model="gpt-3.5-turbo-16k",
|
| 31 |
-
temperature=0.1,
|
| 32 |
-
),
|
| 33 |
system_prompt="You are an AI assistant that uses context from a PDF to assist the user in generating text."
|
| 34 |
)
|
| 35 |
index = VectorStoreIndex.from_documents(docs, service_context=service_context)
|
|
@@ -52,26 +50,22 @@ if st.button("Retrieve"):
|
|
| 52 |
# Use VectorStoreIndex to search
|
| 53 |
query_engine = index.as_query_engine(similarity_top_k=3)
|
| 54 |
st.session_state['retrieved_text'] = query_engine.query(user_query)
|
| 55 |
-
st.write(f"Retrieved Text: {st.session_state['retrieved_text']}")
|
| 56 |
|
| 57 |
# Select content type
|
| 58 |
-
content_type = st.selectbox("Select content type:", ["Blog", "Tweet"])
|
| 59 |
|
| 60 |
# Generate text based on retrieved text and selected content type
|
| 61 |
if st.button("Generate") and content_type:
|
| 62 |
with st.spinner('Generating text...'):
|
| 63 |
-
# Generate text using OpenAI API
|
| 64 |
try:
|
| 65 |
-
if content_type == "Blog"
|
| 66 |
-
prompt = f"Write a blog about 500 words in length using the {st.session_state['retrieved_text']}" #uses content from the stored state
|
| 67 |
-
elif content_type == "Tweet":
|
| 68 |
-
prompt = f"Compose a tweet using the {st.session_state['retrieved_text']}"
|
| 69 |
response = client.chat.completions.create(model="gpt-3.5-turbo-16k",
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
generated_text = response.choices[0].message.content
|
| 75 |
st.write(f"Generated Text: {generated_text}")
|
| 76 |
except Exception as e:
|
| 77 |
-
st.write(f"An error occurred: {e}")
|
|
|
|
| 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")
|
|
|
|
| 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)
|
|
|
|
| 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}")
|