Spaces:
Build error
Build error
Commit
·
7ec5b19
1
Parent(s):
091ee4a
Upload 2 files
Browse files- main.py +64 -0
- requirements.txt +0 -0
main.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from llama_index import VectorStoreIndex, download_loader
|
| 4 |
+
from langchain.agents import initialize_agent, Tool
|
| 5 |
+
from langchain.llms import OpenAI
|
| 6 |
+
from langchain.chains.conversation.memory import ConversationBufferMemory
|
| 7 |
+
from streamlit_chat import message
|
| 8 |
+
|
| 9 |
+
def myApp():
|
| 10 |
+
# Download SimpleWebPageReader
|
| 11 |
+
SimpleWebPageReader = download_loader("SimpleWebPageReader")
|
| 12 |
+
|
| 13 |
+
# Set OpenAI API key
|
| 14 |
+
openai.api_key = "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" # Replace with your actual API key
|
| 15 |
+
|
| 16 |
+
st.header("Chat with Web")
|
| 17 |
+
|
| 18 |
+
# Input for the website URL
|
| 19 |
+
website_url = st.text_input("Website URL", key="url")
|
| 20 |
+
|
| 21 |
+
if website_url:
|
| 22 |
+
try:
|
| 23 |
+
# Initialize SimpleWebPageReader with the provided website URL
|
| 24 |
+
loader = SimpleWebPageReader()
|
| 25 |
+
documents = loader.load_data(urls=[website_url])
|
| 26 |
+
|
| 27 |
+
# Create VectorStoreIndex from documents
|
| 28 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 29 |
+
|
| 30 |
+
# Initialize LangChain OpenAI
|
| 31 |
+
llm = OpenAI(openai_api_key="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", temperature=0, streaming = true)
|
| 32 |
+
|
| 33 |
+
# Initialize ConversationBufferMemory
|
| 34 |
+
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 35 |
+
|
| 36 |
+
# Initialize agent chain
|
| 37 |
+
tools = [
|
| 38 |
+
Tool(
|
| 39 |
+
name="Website Index",
|
| 40 |
+
func=lambda q: index.as_query_engine(),
|
| 41 |
+
description="Useful when you want to answer questions about the text on websites.",
|
| 42 |
+
),
|
| 43 |
+
]
|
| 44 |
+
query_engine = index.as_query_engine()
|
| 45 |
+
|
| 46 |
+
# Get user input for the query
|
| 47 |
+
user_query = st.text_input("Your Question")
|
| 48 |
+
|
| 49 |
+
if st.button("Ask"):
|
| 50 |
+
# Query the LangChain agent with user input
|
| 51 |
+
message(user_query, is_user=True)
|
| 52 |
+
response = query_engine.query(user_query)
|
| 53 |
+
|
| 54 |
+
# Display the response
|
| 55 |
+
st.text("Response:")
|
| 56 |
+
message(str(response))
|
| 57 |
+
|
| 58 |
+
except Exception as e:
|
| 59 |
+
st.error(f"An error occurred: {e}")
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
myApp()
|
requirements.txt
ADDED
|
Binary file (3.91 kB). View file
|
|
|