moazzamdev commited on
Commit
3a1c689
·
1 Parent(s): 8a7a0ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -91
app.py CHANGED
@@ -1,98 +1,47 @@
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
 
96
-
97
- # Display the app
98
- template.servable()
 
 
 
 
 
 
 
 
 
1
  import panel as pn
2
+ import hvplot.pandas
 
 
 
 
 
 
 
 
3
 
4
+ # Load Data
5
+ from bokeh.sampledata.autompg import autompg_clean as df
6
+
7
+ # Make DataFrame Pipeline Interactive
8
+ idf = df.interactive()
9
+
10
+ # Define Panel widgets
11
+ cylinders = pn.widgets.IntSlider(name='Cylinders', start=4, end=8, step=2)
12
+ mfr = pn.widgets.ToggleGroup(
13
+ name='MFR',
14
+ options=['ford', 'chevrolet', 'honda', 'toyota', 'audi'],
15
+ value=['ford', 'chevrolet', 'honda', 'toyota', 'audi'],
16
+ button_type='success')
17
+ yaxis = pn.widgets.RadioButtonGroup(
18
+ name='Y axis',
19
+ options=['hp', 'weight'],
20
+ button_type='success'
21
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
+ # Combine pipeline and widgets
24
+ ipipeline = (
25
+ idf[
26
+ (idf.cyl == cylinders) &
27
+ (idf.mfr.isin(mfr))
28
+ ]
29
+ .groupby(['origin', 'mpg'])[yaxis].mean()
30
+ .to_frame()
31
+ .reset_index()
32
+ .sort_values(by='mpg')
33
+ .reset_index(drop=True)
34
  )
35
 
36
+ # Pipe to hvplot
37
+ ihvplot = ipipeline.hvplot(x='mpg', y=yaxis, by='origin', color=["#ff6f69", "#ffcc5c", "#88d8b0"], line_width=6, height=400)
 
 
 
38
 
39
+ # Layout using Template
40
+ template = pn.template.FastListTemplate(
41
+ title='Interactive DataFrame Dashboards with hvplot .interactive',
42
+ sidebar=[cylinders, 'Manufacturers', mfr, 'Y axis' , yaxis],
43
+ main=[ihvplot.panel()],
44
+ accent_base_color="#88d8b0",
45
+ header_background="#88d8b0",
46
+ )
47
+ template.servable()