mis / app.py
zlmqi's picture
Update app.py
1c99360 verified
raw
history blame
1.31 kB
import openai
import gradio as gr
import os
# retrieve secret key from HF settings
#openai.api_key = os.getenv("OPENAI_API_KEY")
# rebuild storage context and load knowledge index
from llama_index import StorageContext, load_index_from_storage
storage_context = StorageContext.from_defaults(persist_dir='C:/Users/Limin/openai/index')
index = load_index_from_storage(storage_context)
class Chatbot:
def __init__(self, api_key, index):
self.index = index
openai.api_key = api_key
def generate_response(self, user_input):
query_engine = index.as_query_engine()
response = query_engine.query(user_input)
message = {"role": "assistant", "content": response.response}
return message
def create_bot(input):
bot = Chatbot(os.getenv("OPENAI_API_KEY"), index=index)
if input:
response = bot.generate_response(input)
output = response['content']
return output
inputs = gr.Textbox(lines=7, label="Ask questions related to the course.")
outputs = gr.Textbox(label="Reply")
iface = gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="Virtual TA",
description="This is a prototype of learning assistant designed for MIS 320 online section.",
theme="compact")
iface.launch(inline=True)