Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -25,36 +25,48 @@ from langchain import OpenAI
|
|
25 |
from IPython.display import Markdown, display
|
26 |
import gradio as gr
|
27 |
import gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
df = pd.read_excel('Shegardi_dataset.xlsx',sheet_name = 'dataset')
|
29 |
-
os.environ['OPENAI_API_KEY'] = 'sk-
|
|
|
30 |
def construct_index(directory_path):
|
31 |
-
# set maximum input size
|
32 |
max_input_size = 4096
|
33 |
-
# set number of output tokens
|
34 |
num_outputs = 2000
|
35 |
-
# set maximum chunk overlap
|
36 |
max_chunk_overlap = 20
|
37 |
-
# set chunk size limit
|
38 |
chunk_size_limit = 600
|
39 |
|
40 |
-
# define LLM
|
41 |
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.5, model_name="text-davinci-003", max_tokens=num_outputs))
|
42 |
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
|
43 |
-
|
44 |
documents = SimpleDirectoryReader(directory_path).load_data()
|
45 |
-
|
46 |
index = GPTSimpleVectorIndex(
|
47 |
documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper
|
48 |
)
|
49 |
|
50 |
index.save_to_disk('index.json')
|
51 |
|
52 |
-
|
|
|
53 |
|
54 |
-
|
55 |
|
56 |
def ask_ai(query):
|
57 |
-
|
|
|
|
|
|
|
58 |
response = index.query(query, response_mode="compact")
|
59 |
return response.response
|
60 |
|
@@ -68,3 +80,9 @@ iface.launch(share=True)
|
|
68 |
|
69 |
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
from IPython.display import Markdown, display
|
26 |
import gradio as gr
|
27 |
import gradio
|
28 |
+
import pandas as pd
|
29 |
+
from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
|
30 |
+
from langchain import OpenAI
|
31 |
+
import sys
|
32 |
+
import os
|
33 |
+
from IPython.display import Markdown, display
|
34 |
+
import pandas as pd
|
35 |
+
from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
|
36 |
+
from langchain import OpenAI
|
37 |
+
from IPython.display import Markdown, display
|
38 |
+
import streamlit as st
|
39 |
+
import pickle
|
40 |
df = pd.read_excel('Shegardi_dataset.xlsx',sheet_name = 'dataset')
|
41 |
+
os.environ['OPENAI_API_KEY'] = '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'
|
42 |
+
|
43 |
def construct_index(directory_path):
|
|
|
44 |
max_input_size = 4096
|
|
|
45 |
num_outputs = 2000
|
|
|
46 |
max_chunk_overlap = 20
|
|
|
47 |
chunk_size_limit = 600
|
48 |
|
|
|
49 |
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.5, model_name="text-davinci-003", max_tokens=num_outputs))
|
50 |
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
|
51 |
+
|
52 |
documents = SimpleDirectoryReader(directory_path).load_data()
|
53 |
+
|
54 |
index = GPTSimpleVectorIndex(
|
55 |
documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper
|
56 |
)
|
57 |
|
58 |
index.save_to_disk('index.json')
|
59 |
|
60 |
+
with open('llm_predictor.pkl', 'wb') as f:
|
61 |
+
pickle.dump(llm_predictor, f)
|
62 |
|
63 |
+
return index
|
64 |
|
65 |
def ask_ai(query):
|
66 |
+
with open('llm_predictor.pkl', 'rb') as f:
|
67 |
+
llm_predictor = pickle.load(f)
|
68 |
+
|
69 |
+
index = GPTSimpleVectorIndex.load_from_disk('index.json', llm_predictor=llm_predictor)
|
70 |
response = index.query(query, response_mode="compact")
|
71 |
return response.response
|
72 |
|
|
|
80 |
|
81 |
|
82 |
|
83 |
+
|
84 |
+
|
85 |
+
|
86 |
+
|
87 |
+
|
88 |
+
|