productizationlabs
commited on
Commit
·
c18ef0a
1
Parent(s):
7ee2680
Upload 3 files
Browse files- app.py +92 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from gpt_index import SimpleDirectoryReader, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
|
3 |
+
from langchain import OpenAI
|
4 |
+
import gradio as gr
|
5 |
+
import openai
|
6 |
+
|
7 |
+
API_URL = "https://api.openai.com/v1/chat/completions"
|
8 |
+
|
9 |
+
openai.api_key = os.environ["OPENAI_API_KEY"]
|
10 |
+
|
11 |
+
top_p_chatgpt = 1.0
|
12 |
+
temperature_chatgpt = 1.0
|
13 |
+
|
14 |
+
def predict_chatgpt(inputs,chat_counter_chatgpt, chatbot_chatgpt=[], history=[]):
|
15 |
+
if chat_counter_chatgpt != 0:
|
16 |
+
messages = []
|
17 |
+
for data in chatbot_chatgpt:
|
18 |
+
temp1 = {}
|
19 |
+
temp1["role"] = "user"
|
20 |
+
temp1["content"] = data[0]
|
21 |
+
temp2 = {}
|
22 |
+
temp2["role"] = "assistant"
|
23 |
+
temp2["content"] = data[1]
|
24 |
+
messages.append(temp1)
|
25 |
+
messages.append(temp2)
|
26 |
+
temp3 = {}
|
27 |
+
temp3["role"] = "user"
|
28 |
+
temp3["content"] = inputs
|
29 |
+
messages.append(temp3)
|
30 |
+
|
31 |
+
#os.environ['OPENAI_API_KEY'] = openai.api_key
|
32 |
+
chat_counter_chatgpt += 1
|
33 |
+
history.append("You asked: " + inputs)
|
34 |
+
|
35 |
+
# load index from disk
|
36 |
+
index = GPTSimpleVectorIndex.load_from_disk('PLIndex.json')
|
37 |
+
|
38 |
+
# query the index
|
39 |
+
result = index.query(inputs)
|
40 |
+
response = result.response.split()
|
41 |
+
token_counter = 0
|
42 |
+
partial_words = ""
|
43 |
+
counter = 0
|
44 |
+
for chunk in response:
|
45 |
+
partial_words=partial_words+" "+chunk
|
46 |
+
if token_counter == 0:
|
47 |
+
history.append(" " + partial_words)
|
48 |
+
else:
|
49 |
+
history[-1] = partial_words
|
50 |
+
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)] # convert to tuples of list
|
51 |
+
token_counter += 1
|
52 |
+
yield chat, history, chat_counter_chatgpt # This resembles {chatbot: chat, state: history}
|
53 |
+
|
54 |
+
|
55 |
+
def reset_textbox():
|
56 |
+
return gr.update(value="")
|
57 |
+
|
58 |
+
def reset_chat(chatbot, state):
|
59 |
+
return None, []
|
60 |
+
with gr.Blocks(css="""#col_container {width: 1000px; margin-left: auto; margin-right: auto;}
|
61 |
+
#chatgpt {height: 400px; overflow: auto;}} """, theme=gr.themes.Default(primary_hue="slate") ) as PLCoversationalAI:
|
62 |
+
with gr.Row():
|
63 |
+
with gr.Column(scale=14):
|
64 |
+
with gr.Box():
|
65 |
+
with gr.Row():
|
66 |
+
with gr.Column(scale=13):
|
67 |
+
inputs = gr.Textbox(label="Ask anything about Productization Labs ⤵️ Try : Who is Gopala" )
|
68 |
+
with gr.Column(scale=1):
|
69 |
+
b1 = gr.Button('Submit', elem_id = 'submit').style(full_width=True)
|
70 |
+
b2 = gr.Button('Clear', elem_id = 'clear').style(full_width=True)
|
71 |
+
state_chatgpt = gr.State([])
|
72 |
+
|
73 |
+
with gr.Box():
|
74 |
+
with gr.Row():
|
75 |
+
chatbot_chatgpt = gr.Chatbot(elem_id="chatgpt", label="Productization Labs Conversational AI")
|
76 |
+
chat_counter_chatgpt = gr.Number(value=0, visible=False, precision=0)
|
77 |
+
|
78 |
+
|
79 |
+
inputs.submit(reset_textbox, [], [inputs])
|
80 |
+
|
81 |
+
b1.click( predict_chatgpt,
|
82 |
+
[ inputs, chat_counter_chatgpt, chatbot_chatgpt, state_chatgpt],
|
83 |
+
[chatbot_chatgpt, state_chatgpt],)
|
84 |
+
|
85 |
+
b2.click(reset_chat, [chatbot_chatgpt, state_chatgpt], [chatbot_chatgpt, state_chatgpt])
|
86 |
+
|
87 |
+
PLCoversationalAI.queue(concurrency_count=16).launch(height= 2500, debug=True)
|
88 |
+
|
89 |
+
|
90 |
+
|
91 |
+
|
92 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
gpt_index
|
3 |
+
langchain
|
4 |
+
transformers
|