File size: 1,911 Bytes
593df94 |
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 60 61 62 63 64 |
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# -------------------------
# HuggingFace model to use
# -------------------------
MODEL_NAME = "tiiuae/falcon-7b-instruct" # you can change to any hosted model
# -------------------------
# Load model and tokenizer
# -------------------------
print("Loading model...")
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
# -------------------------
# System prompt
# -------------------------
SYSTEM_PROMPT = (
"You are a helpful, creative AI assistant. "
"Your creator is Austin. Answer clearly and politely."
)
# -------------------------
# Chat function
# -------------------------
def chat_with_ai(user_input, history=[]):
full_prompt = SYSTEM_PROMPT + "\n"
for i, (u, r) in enumerate(history):
full_prompt += f"User: {u}\nAI: {r}\n"
full_prompt += f"User: {user_input}\nAI:"
inputs = tokenizer(full_prompt, return_tensors="pt").to(device)
outputs = model.generate(**inputs, max_new_tokens=200)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
response = response.split("AI:")[-1].strip()
history.append((user_input, response))
return response, history
# -------------------------
# Build Gradio GUI
# -------------------------
with gr.Blocks() as demo:
gr.Markdown("# Austin's AI Chatbot")
gr.Markdown("This chatbot was created by **Austin**. Chat with it below!")
chatbot = gr.Chatbot()
user_input = gr.Textbox(placeholder="Type your message here...")
submit_btn = gr.Button("Send")
history_state = gr.State([])
submit_btn.click(
chat_with_ai,
inputs=[user_input, history_state],
outputs=[chatbot, history_state]
)
# Launch app
demo.launch()
|