|
import gradio as gr
|
|
from commons.Model import model
|
|
from commons.Configs import configs
|
|
from commons.OpenAIClient import openaiClient
|
|
from prepareutils.Dataset import dataset
|
|
import numpy as np
|
|
import openai
|
|
|
|
|
|
clf = model.load()
|
|
|
|
qaDataset = dataset.loadDataset()
|
|
|
|
|
|
def predict(question, openaiKey):
|
|
|
|
configs.OPENAI_KEY = openaiKey
|
|
openai.api_key = openaiKey
|
|
|
|
questionEmbedding = openaiClient.generateEmbeddings([question])[0]
|
|
|
|
answerIndex = clf.predict([questionEmbedding]).item()
|
|
|
|
bestAnswer = qaDataset[answerIndex]
|
|
return bestAnswer["answer"]
|
|
|
|
|
|
def randomExamples(numberOfExamples=15):
|
|
|
|
randomIndexes = np.random.randint(0, len(qaDataset), numberOfExamples)
|
|
examples = []
|
|
for index in randomIndexes:
|
|
question = qaDataset[index]["question"]
|
|
examples.append([question])
|
|
return examples
|
|
|
|
|
|
gr.Interface(
|
|
fn=predict,
|
|
inputs=["text", "text"],
|
|
outputs="text",
|
|
examples=randomExamples(),
|
|
).launch()
|
|
|