Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import os.path
|
4 |
+
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, load_index_from_storage, ServiceContext, StorageContext
|
7 |
+
from llama_index.readers.file import PDFReader
|
8 |
+
from llama_index.llms.openai import OpenAI
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
from llama_index.core.tools import QueryEngineTool, ToolMetadata
|
13 |
+
from llama_index.agent.openai import OpenAIAgent
|
14 |
+
|
15 |
+
|
16 |
+
load_dotenv()
|
17 |
+
storage_path = "./vectorstore"
|
18 |
+
documents_path = "./documents"
|
19 |
+
|
20 |
+
llm = OpenAI(model="gpt-3.5-turbo")
|
21 |
+
service_context = ServiceContext.from_defaults(llm=llm)
|
22 |
+
|
23 |
+
@st.cache_resource(show_spinner=False)
|
24 |
+
def initialize():
|
25 |
+
if not os.path.exists(storage_path):
|
26 |
+
documents = SimpleDirectoryReader(documents_path).load_data()
|
27 |
+
index = VectorStoreIndex.from_documents(documents)
|
28 |
+
index.storage_context.persist(persist_dir=storage_path)
|
29 |
+
else:
|
30 |
+
storage_context = StorageContext.from_defaults(persist_dir=storage_path)
|
31 |
+
index = load_index_from_storage(storage_context)
|
32 |
+
return index
|
33 |
+
index = initialize()
|
34 |
+
|
35 |
+
fitness_engine = index.as_query_engine(similarity_top_k=3,similarity_cutoff=0.5)
|
36 |
+
|
37 |
+
tools = [
|
38 |
+
|
39 |
+
QueryEngineTool(
|
40 |
+
query_engine=fitness_engine,
|
41 |
+
metadata=ToolMetadata(
|
42 |
+
name="fitnessdata",
|
43 |
+
description="this gives information about diets , workoutplan and mental health",
|
44 |
+
),
|
45 |
+
)]
|
46 |
+
agent = OpenAIAgent.from_tools(tools, verbose=True)
|
47 |
+
|
48 |
+
# # Retrieve input (e.g., from user input)
|
49 |
+
# input_text = "what are your views on processed food?"
|
50 |
+
|
51 |
+
# # Query the index
|
52 |
+
# results = agent.query(input_text)
|
53 |
+
|
54 |
+
# # Process results (e.g., select the most relevant result)
|
55 |
+
# if results:
|
56 |
+
# response = results
|
57 |
+
# else:
|
58 |
+
# response = "Sorry, I couldn't find a relevant response."
|
59 |
+
|
60 |
+
# # Display response
|
61 |
+
# print("Response:", response)
|
62 |
+
st.title("Fitness App")
|
63 |
+
input_text = st.text_input("Enter your question here:")
|
64 |
+
if st.button("Ask"):
|
65 |
+
if input_text:
|
66 |
+
# Query the index
|
67 |
+
results = agent.query(input_text)
|
68 |
+
|
69 |
+
# Process results
|
70 |
+
if results:
|
71 |
+
response = results.response
|
72 |
+
else:
|
73 |
+
response = "Sorry, I couldn't find a relevant response."
|
74 |
+
|
75 |
+
# Display response
|
76 |
+
st.write("Response:", response)
|