Spaces:
Runtime error
Runtime error
lukaslazarcik
commited on
Commit
•
192ef5e
1
Parent(s):
54442ff
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
from llama_index import ServiceContext, download_loader, GPTVectorStoreIndex, LLMPredictor
|
3 |
+
from llama_index import StorageContext, load_index_from_storage
|
4 |
+
from langchain import OpenAI
|
5 |
+
import gradio as gr
|
6 |
+
import os
|
7 |
+
|
8 |
+
# My personal OpenAI API key - do not misuse :P
|
9 |
+
os.environ["OPENAI_API_KEY"] = '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'
|
10 |
+
|
11 |
+
num_outputs = 512
|
12 |
+
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.4, model_name="text-davinci-003", max_tokens=num_outputs))
|
13 |
+
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)
|
14 |
+
|
15 |
+
def construct_index():
|
16 |
+
SimpleCSVReader = download_loader("SimpleCSVReader")
|
17 |
+
loader = SimpleCSVReader()
|
18 |
+
docs = loader.load_data(file=Path('./docs/articles.csv'))
|
19 |
+
|
20 |
+
index = GPTVectorStoreIndex.from_documents(docs, service_context=service_context)
|
21 |
+
|
22 |
+
index.storage_context.persist()
|
23 |
+
|
24 |
+
return index
|
25 |
+
|
26 |
+
def chatbot(input_text):
|
27 |
+
storage_context = StorageContext.from_defaults(persist_dir=Path("./storage/"))
|
28 |
+
index = load_index_from_storage(storage_context, service_context=service_context)
|
29 |
+
query_engine = index.as_query_engine(response_mode='compact')
|
30 |
+
response = query_engine.query(input_text)
|
31 |
+
return response.response
|
32 |
+
|
33 |
+
# uncomment on first run, when generating an index
|
34 |
+
index = construct_index()
|
35 |
+
|
36 |
+
iface = gr.Interface(fn=chatbot,
|
37 |
+
inputs=gr.inputs.Textbox(lines=7, label="Enter your text"),
|
38 |
+
outputs="text",
|
39 |
+
title="147 AI Chatbot")
|
40 |
+
|
41 |
+
|
42 |
+
iface.launch()
|
43 |
+
|
44 |
+
# with gr.Blocks() as demo:
|
45 |
+
# chatbot = gr.Chatbot()
|
46 |
+
# msg = gr.Textbox()
|
47 |
+
# clear = gr.Button("Clear")
|
48 |
+
|
49 |
+
# def respond(message, chat_history):
|
50 |
+
# storage_context = StorageContext.from_defaults(persist_dir=Path("./storage/"))
|
51 |
+
# index = load_index_from_storage(storage_context, service_context=service_context)
|
52 |
+
# query_engine = index.as_query_engine(response_mode='compact')
|
53 |
+
# response = query_engine.query(message)
|
54 |
+
# chat_history.append((message, response))
|
55 |
+
# return "", chat_history
|
56 |
+
|
57 |
+
# msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
58 |
+
# clear.click(lambda: None, None, chatbot, queue=False)
|
59 |
+
|
60 |
+
# demo.launch()
|