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()