moazzamdev commited on
Commit
e6f77e4
·
1 Parent(s): 0c3f691

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +108 -0
  3. requirements.txt +113 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+ RUN python3 -m pip install --no-cache-dir --upgrade pip
7
+ RUN python3 -m pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ COPY . .
10
+
11
+ CMD ["panel", "serve", "/code/app.py", "--address", "0.0.0.0", "--port", "7860", "--allow-websocket-origin", "*"]
12
+
13
+ RUN mkdir /.cache
14
+ RUN chmod 777 /.cache
15
+ RUN mkdir .chroma
16
+ RUN chmod 777 .chroma
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import hvplot.pandas
2
+ import numpy as np
3
+ import panel as pn
4
+ import pandas as pd
5
+ import openai
6
+ from llama_index import VectorStoreIndex, download_loader
7
+ from langchain.agents import initialize_agent, Tool
8
+ from langchain.llms import OpenAI
9
+ from langchain.chains.conversation.memory import ConversationBufferMemory
10
+ from panel.chat import ChatInterface
11
+ import time
12
+ pn.extension("perspective")
13
+
14
+ def callback(contents: str, user: str, instance: pn.chat.ChatInterface):
15
+ message = f"Echoing {user}: {contents}"
16
+ return message
17
+ chat_interface = pn.chat.ChatInterface(callback=callback)
18
+ msg_panel = chat_interface.send(
19
+ "Enter a WEB link and ask anything!-\nNote: images in the link will be ignored!!!",
20
+ user="assistant",
21
+ respond=False,
22
+ )
23
+ apikey = pn.widgets.TextInput(name='OPENAI API KEY', placeholder="sk-********")
24
+ apply = pn.widgets.Button(name='Apply', button_type='default')
25
+ website_url_input = pn.widgets.TextInput(name='Website URL', placeholder="https://www.google.com/")
26
+ submit = pn.widgets.Button(name='Submit', button_type='primary')
27
+
28
+
29
+ def on_submit(event, contents, ):
30
+
31
+ try:
32
+ SimpleWebPageReader = download_loader("SimpleWebPageReader")
33
+
34
+ # Set OpenAI API key
35
+ openai.api_key = apikey.value # Replace with your actual API key
36
+
37
+ # Get the entered website URL
38
+ website_url = website_url_input.value
39
+
40
+ if website_url:
41
+ # Initialize SimpleWebPageReader with the provided website URL
42
+ loader = SimpleWebPageReader()
43
+ documents = loader.load_data(urls=[website_url])
44
+
45
+ # Create VectorStoreIndex from documents
46
+ index = VectorStoreIndex.from_documents(documents)
47
+
48
+ # Initialize LangChain OpenAI
49
+ index = VectorStoreIndex.from_documents(documents)
50
+ llm = OpenAI(openai_api_key=apikey.value, temperature=0, streaming = True
51
+ )
52
+
53
+ # Initialize ConversationBufferMemory
54
+ memory = ConversationBufferMemory(memory_key="chat_history")
55
+
56
+ # Initialize agent chain
57
+ tools = [
58
+ Tool(
59
+ name="Website Index",
60
+ func=lambda q: index.as_query_engine(),
61
+ description="Useful when you want to answer questions about the text on websites.",
62
+ ),
63
+ ]
64
+
65
+
66
+ query_engine = index.as_query_engine()
67
+ response = query_engine.query(contents)
68
+
69
+ return str(response),
70
+
71
+ except Exception as e:
72
+ print(f"Error: {e}")
73
+ def even_or_odd(contents, user, instance):
74
+ response_tuple = on_submit(event='', contents=contents)
75
+
76
+ # Extracting the first element of the tuple and converting it to a string
77
+ response_string = str(response_tuple)
78
+
79
+ return response_string
80
+ # Set the callback function for the button click event
81
+ submit.on_click(on_submit)
82
+
83
+ # Instantiate the template with widgets displayed in the sidebar
84
+ template = pn.template.FastListTemplate(
85
+ title='Chat with Web',
86
+ sidebar=[apikey,apply,website_url_input, submit,
87
+ msg_panel],
88
+ header=[],
89
+
90
+ )
91
+
92
+ ChatInterface(callback=even_or_odd)
93
+ def callback(contents: str, user: str, instance: pn.chat.ChatInterface):
94
+ message = query_engine.query(contents)
95
+ return message
96
+
97
+ template.main.append(
98
+
99
+ ChatInterface(
100
+ callback=even_or_odd,
101
+ user="User",
102
+ avatar="🧑",
103
+ callback_user="System",
104
+ )
105
+ )
106
+
107
+ # Display the app
108
+ template.servable()
requirements.txt ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ panel
2
+ jupyter
3
+ transformers
4
+ numpy
5
+ torch
6
+ aiohttp
7
+ aiohttp==3.9.1
8
+ aiosignal==1.3.1
9
+ altair==5.2.0
10
+ annotated-types==0.6.0
11
+ anyio==4.2.0
12
+ async-timeout==4.0.3
13
+ attrs==23.1.0
14
+ beautifulsoup4==4.12.2
15
+ bleach==6.1.0
16
+ blinker==1.7.0
17
+ bokeh==3.3.2
18
+ cachetools==5.3.2
19
+ certifi==2023.11.17
20
+ charset-normalizer==3.3.2
21
+ click==8.1.7
22
+ colorama==0.4.6
23
+ colorcet==3.0.1
24
+ contourpy==1.2.0
25
+ dataclasses-json==0.6.3
26
+ Deprecated==1.2.14
27
+ distro==1.8.0
28
+ exceptiongroup==1.2.0
29
+ frozenlist==1.4.1
30
+ fsspec==2023.12.2
31
+ gitdb==4.0.11
32
+ GitPython==3.1.40
33
+ greenlet==3.0.3
34
+ h11==0.14.0
35
+ holoviews==1.18.1
36
+ html2text==2020.1.16
37
+ httpcore==1.0.2
38
+ httpx==0.26.0
39
+ hvplot==0.9.1
40
+ idna==3.6
41
+ importlib-metadata==6.11.0
42
+ Jinja2==3.1.2
43
+ joblib==1.3.2
44
+ jsonpatch==1.33
45
+ jsonpointer==2.4
46
+ jsonschema==4.20.0
47
+ jsonschema-specifications==2023.11.2
48
+ langchain==0.0.352
49
+ langchain-community==0.0.6
50
+ langchain-core==0.1.3
51
+ langsmith==0.0.74
52
+ linkify-it-py==2.0.2
53
+ llama-index==0.9.21
54
+ Markdown==3.5.1
55
+ markdown-it-py==3.0.0
56
+ MarkupSafe==2.1.3
57
+ marshmallow==3.20.1
58
+ mdit-py-plugins==0.4.0
59
+ mdurl==0.1.2
60
+ multidict==6.0.4
61
+ mypy-extensions==1.0.0
62
+ nest-asyncio==1.5.8
63
+ nltk==3.8.1
64
+ numpy==1.26.2
65
+ openai==1.6.1
66
+ packaging==23.2
67
+ pandas==2.1.4
68
+ panel==1.3.6
69
+ param==2.0.1
70
+ Pillow==10.1.0
71
+ protobuf==4.25.1
72
+ pyarrow==14.0.2
73
+ pyct==0.5.0
74
+ pydantic==2.5.3
75
+ pydantic_core==2.14.6
76
+ pydeck==0.8.1b0
77
+ Pygments==2.17.2
78
+ python-dateutil==2.8.2
79
+ pytz==2023.3.post1
80
+ pyviz_comms==3.0.0
81
+ PyYAML==6.0.1
82
+ rank-bm25==0.2.2
83
+ referencing==0.32.0
84
+ regex==2023.10.3
85
+ requests==2.31.0
86
+ rich==13.7.0
87
+ rpds-py==0.15.2
88
+ six==1.16.0
89
+ smmap==5.0.1
90
+ sniffio==1.3.0
91
+ soupsieve==2.5
92
+ SQLAlchemy==2.0.23
93
+ streamlit==1.29.0
94
+ streamlit-chat==0.1.1
95
+ tenacity==8.2.3
96
+ tiktoken==0.5.2
97
+ toml==0.10.2
98
+ toolz==0.12.0
99
+ tornado==6.4
100
+ tqdm==4.66.1
101
+ typing-inspect==0.9.0
102
+ typing_extensions==4.9.0
103
+ tzdata==2023.3
104
+ tzlocal==5.2
105
+ uc-micro-py==1.0.2
106
+ urllib3==2.1.0
107
+ validators==0.22.0
108
+ watchdog==3.0.0
109
+ webencodings==0.5.1
110
+ wrapt==1.16.0
111
+ xyzservices==2023.10.1
112
+ yarl==1.9.4
113
+ zipp==3.17.0