Download app.py from CPUA/ARCHIE: direct link, hf CLI and curl.
- Browser
- Download file 1.41 kB
-
https://huggingface.co/spaces/CPUA/ARCHIE/resolve/main/app.py
- Command line
-
hf download hf://spaces/CPUA/ARCHIE/app.py
-
curl -L -o app.py https://huggingface.co/spaces/CPUA/ARCHIE/resolve/main/app.py
1.41 kB
| 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() |