File size: 1,413 Bytes
0f345b7 da1e9a0 0f345b7 da1e9a0 0f345b7 da1e9a0 0f345b7 da1e9a0 0f345b7 da1e9a0 0f345b7 da1e9a0 | 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 | import gradio as gr
import json
from transformers import AutoModelForCausalLM, AutoTokenizer
# Load the GPT-J model and tokenizer
model_name = "EleutherAI/gpt-j-6B"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Load the Book of ARCHIE JSON file
with open("book_of_archie.json", "r") as f:
knowledge_base = json.load(f)
# Define a response function that searches the knowledge base
def chatbot_response(input_text):
# Search the knowledge base for relevant answers
response = ""
for topic, texts in knowledge_base.items():
for text in texts:
if input_text.lower() in text.lower():
response += text + "\n\n"
# Fallback to GPT-J if no match is found in the knowledge base
if not response:
inputs = tokenizer(input_text, return_tensors="pt")
outputs = model.generate(inputs.input_ids, max_length=150, num_return_sequences=1)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
return response.strip()
# Create the Gradio interface
interface = gr.Interface(
fn=chatbot_response,
inputs=gr.Textbox(lines=5, label="Ask ARCHIE a question"),
outputs="text",
title="ARCHIE Chatbot",
description="Talk to ARCHIE about politics, history, or anything on your mind!",
theme="default"
)
# Launch the interface
interface.launch() |