Spaces:
Runtime error
Runtime error
File size: 4,871 Bytes
b753024 842e7d3 b753024 0c1e154 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | import os
from llama_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
import gradio as gr
import openai
from langchain import OpenAI
os.environ["OPENAI_API_KEY"] = os.environ["OPENAI_API_KEY"]
openai_engines = ["text-davinci-003", "code-davinci-002", "text-curie-001"]
prompt = ""
def construct_index(directory_path):
max_input_size = 4096
num_outputs = 512
max_chunk_overlap = 20
chunk_size_limit = 600
prompt_helper = PromptHelper(
max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
llm_predictor = LLMPredictor(llm=OpenAI(
temperature=0.3, model_name="text-davinci-003", max_tokens=num_outputs))
documents = SimpleDirectoryReader(directory_path).load_data()
index = GPTSimpleVectorIndex(
documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
index.save_to_disk('index.json')
return index
def chatbot_local(input_text):
index = GPTSimpleVectorIndex.load_from_disk('index.json')
query_configs = [
{
"index_struct_type": "simple_dict",
"query_mode": "default",
"query_kwargs": {
"similarity_top_k": 1,
# "include_summary": True
},
},
{
"index_struct_type": "list",
"query_mode": "default",
"query_kwargs": {
"response_mode": "tree_summarize",
"verbose": True
}
},
]
response = index.query(input_text, mode="recursive", response_mode="compact", query_configs = query_configs)
return response.response
def openai_completion(
prompt,
openai_token=None,
engine="text-davinci-003",
temperature=0.9,
max_tokens=150,
top_p=1,
frequency_penalty=0,
presence_penalty=0.6,
stop=[" Human:", " AI:"],
):
openai.api_key = openai_token
response = openai.Completion.create(
engine=engine,
prompt=prompt,
temperature=temperature,
max_tokens=max_tokens,
top_p=top_p,
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty,
stop=stop,
)
return response.choices[0].text
def chatgpt3(
prompt,
history,
):
history = history or []
# history_prompt = list(sum(history, ()))
# history_prompt.append(f"\nHuman: {prompt}")
# inp = " ".join(history_prompt)
# keep the prompt length limited to ~2000 tokens
# create the output with openai
out = chatbot_local(prompt)
history.append((prompt, out))
return history, history, ""
with gr.Blocks(title="ASDOS") as block:
gr.Markdown("## ASDOS")
with gr.Row():
# with gr.Column():
# openai_token = gr.Textbox(label="OpenAI API Key", value=os.getenv("OPENAI_API_KEY"))
# engine = gr.Dropdown(
# label="GPT3 Engine",
# choices=openai_engines,
# value="text-davinci-003",
# )
# temperature = gr.Slider(label="Temperature", minimum=0, maximum=1, step=0.1, value=0.9)
# max_tokens = gr.Slider(label="Max Tokens", minimum=10, maximum=400, step=10, value=150)
# top_p = gr.Slider(label="Top P", minimum=0, maximum=1, step=0.1, value=1)
# frequency_penalty = gr.Slider(
# label="Frequency Penalty",
# minimum=0,
# maximum=1,
# step=0.1,
# value=0,
# )
# presence_penalty = gr.Slider(
# label="Presence Penalty",
# minimum=0,
# maximum=1,
# step=0.1,
# value=0.6,
# )
with gr.Column():
chatbot = gr.Chatbot()
message = gr.Textbox(value=prompt,
label="Saya adalah asisten dosen kuncie", placeholder="Minta tolong apapun yg berkaitan dengan kelas ini")
state = gr.State()
message.submit(
fn=chatgpt3,
inputs=[
message,
state,
],
outputs=[chatbot, state, message],
)
submit = gr.Button("Send")
submit.click(
chatgpt3,
inputs=[
message,
state,
# openai_token,
# engine,
# temperature,
# max_tokens,
# top_p,
# frequency_penalty,
# presence_penalty,
],
outputs=[chatbot, state, message],
)
if __name__ == "__main__":
index = construct_index("docs")
block.launch(debug=True) |