AhmedEwis commited on
Commit
f4a091c
1 Parent(s): 2cff6e2

initial commit

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Copy of Copy of Chatbot with custom knowledge base
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1VSXUmag_76fzebs16YhW_as4mdhHNdkx
8
+ """
9
+
10
+ !pip install llama-index
11
+ !pip install langchain
12
+ !pip install gradio
13
+ !pip install pandas
14
+ !pip install openpyxl
15
+
16
+ import pandas as pd
17
+ from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
18
+ from langchain import OpenAI
19
+ import sys
20
+ import os
21
+ from IPython.display import Markdown, display
22
+ import pandas as pd
23
+ from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
24
+ 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'] = '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'
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
+ return index
53
+
54
+ def ask_ai():
55
+ index = GPTSimpleVectorIndex.load_from_disk('index.json')
56
+ while True:
57
+ query = input("What do you want to ask? ")
58
+ response = index.query(query, response_mode="compact")
59
+ display(Markdown(f"Response: <b>{response.response}</b>")
60
+
61
+
62
+ construct_index("context_data/data")
63
+
64
+ def ask_ai(query):
65
+ index = GPTSimpleVectorIndex.load_from_disk('index.json')
66
+ response = index.query(query, response_mode="compact")
67
+ return response.response
68
+
69
+ iface = gr.Interface(fn=ask_ai, inputs="text", outputs="text", title="The following is a conversation with a human called Shegardi. Shegardi is helpful, precise, truthful, and very friendly.  Also, Shegardi is an employee of Warba Bank, located in Kuwait. Shegardi will only use the information provided to him. ",
70
+ description="Enter a question and get an answer from Shegardi.")
71
+ iface.launch(share=True)
72
+
73
+
74
+
75
+
76
+
77
+
78
+