Commit
·
8318ab3
1
Parent(s):
b9887fb
Upload 3 files
Browse files- Dockerfile +11 -0
- app.py +106 -0
- requirements.txt +0 -0
Dockerfile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
|
| 3 |
+
WORKDIR /code
|
| 4 |
+
|
| 5 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 6 |
+
|
| 7 |
+
RUN 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", "moazzamdev-Chat_with_web_using_panel.hf.space", "--allow-websocket-origin", "0.0.0.0:7860"]
|
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
website_url_input = pn.widgets.TextInput(name='Website URL', placeholder="https://www.google.com/")
|
| 24 |
+
submit = pn.widgets.Button(name='Submit', button_type='primary')
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def on_submit(event, contents, ):
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
SimpleWebPageReader = download_loader("SimpleWebPageReader")
|
| 31 |
+
|
| 32 |
+
# Set OpenAI API key
|
| 33 |
+
openai.api_key = "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" # Replace with your actual API key
|
| 34 |
+
|
| 35 |
+
# Get the entered website URL
|
| 36 |
+
website_url = website_url_input.value
|
| 37 |
+
|
| 38 |
+
if website_url:
|
| 39 |
+
# Initialize SimpleWebPageReader with the provided website URL
|
| 40 |
+
loader = SimpleWebPageReader()
|
| 41 |
+
documents = loader.load_data(urls=[website_url])
|
| 42 |
+
|
| 43 |
+
# Create VectorStoreIndex from documents
|
| 44 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 45 |
+
|
| 46 |
+
# Initialize LangChain OpenAI
|
| 47 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 48 |
+
llm = OpenAI(openai_api_key="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", temperature=0, streaming = True
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Initialize ConversationBufferMemory
|
| 52 |
+
memory = ConversationBufferMemory(memory_key="chat_history")
|
| 53 |
+
|
| 54 |
+
# Initialize agent chain
|
| 55 |
+
tools = [
|
| 56 |
+
Tool(
|
| 57 |
+
name="Website Index",
|
| 58 |
+
func=lambda q: index.as_query_engine(),
|
| 59 |
+
description="Useful when you want to answer questions about the text on websites.",
|
| 60 |
+
),
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
query_engine = index.as_query_engine()
|
| 65 |
+
response = query_engine.query(contents)
|
| 66 |
+
|
| 67 |
+
return str(response),
|
| 68 |
+
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(f"Error: {e}")
|
| 71 |
+
def even_or_odd(contents, user, instance):
|
| 72 |
+
response_tuple = on_submit(event='', contents=contents)
|
| 73 |
+
|
| 74 |
+
# Extracting the first element of the tuple and converting it to a string
|
| 75 |
+
response_string = str(response_tuple)
|
| 76 |
+
|
| 77 |
+
return response_string
|
| 78 |
+
# Set the callback function for the button click event
|
| 79 |
+
submit.on_click(on_submit)
|
| 80 |
+
|
| 81 |
+
# Instantiate the template with widgets displayed in the sidebar
|
| 82 |
+
template = pn.template.BootstrapTemplate(
|
| 83 |
+
title='Chat with Web',
|
| 84 |
+
sidebar=[website_url_input, submit,
|
| 85 |
+
msg_panel],
|
| 86 |
+
header=[],
|
| 87 |
+
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
ChatInterface(callback=even_or_odd)
|
| 91 |
+
def callback(contents: str, user: str, instance: pn.chat.ChatInterface):
|
| 92 |
+
message = query_engine.query(contents)
|
| 93 |
+
return message
|
| 94 |
+
|
| 95 |
+
template.main.append(
|
| 96 |
+
|
| 97 |
+
ChatInterface(
|
| 98 |
+
callback=even_or_odd,
|
| 99 |
+
user="User",
|
| 100 |
+
avatar="🧑",
|
| 101 |
+
callback_user="System",
|
| 102 |
+
),
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Display the app
|
| 106 |
+
template.servable()
|
requirements.txt
ADDED
|
Binary file (3.91 kB). View file
|
|
|