Spaces:
Runtime error
Runtime error
File size: 1,867 Bytes
4f6a35e 3b867b6 4f6a35e b001cd4 4f6a35e 2c2e83a 4f6a35e 2c2e83a 4f6a35e 3b867b6 |
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 |
import openai
import json
import gradio as gr
import os
from openai import OpenAI
#from llama_index import set_global_service_context
#set_global_service_context(service_context)
# retrieve secret key from HF settings
#openai.api_key = os.getenv("OPENAI_API_KEY")
# rebuild storage context and load knowledge index
from llama_index.core import StorageContext, load_index_from_storage
storage_context = StorageContext.from_defaults(persist_dir='./')
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("sk-proj-izWrxKNpJzcpi7BLTAz1T3BlbkFJQKPvwCzS9GmWBwD0cPE3"), index=index)
# use moderations endpoint to check input
if input:
client = OpenAI()
response_mod = client.moderations.create(input=input)
response_dict = response_mod.model_dump()
flagged = response_dict['results'][0]['flagged']
#print("Flagged:", flagged)
if not flagged:
response_bot = bot.generate_response(input)
output = response_bot['content']
else:
output = "Invalid request."
return output
inputs = gr.Textbox(lines=7, label="Ask questions related to the course.")
outputs = gr.Textbox(label="Reply")
iface = gr.Interface(fn=create_bot, inputs=inputs, outputs=outputs, title="Virtual TA",
description="This is a prototype of learning assistant designed for marketing courses. Powered by ChatGPT 3.5.",
theme="compact")
iface.launch(inline=True)
|