File size: 1,427 Bytes
0451733 8340f66 0451733 8340f66 3e5af81 8340f66 0451733 3e5af81 8340f66 0451733 8340f66 0451733 8340f66 0451733 8340f66 | 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
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# Load model and tokenizer
model_name = "microsoft/DialoGPT-small" # ✅ open-access, no login required
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float32)
device = "cpu"
def chat_with_jarvis(messages, history):
# Convert message list to a single prompt string
conversation = ""
for msg in messages:
role = msg["role"]
content = msg["content"]
if role == "user":
conversation += f"User: {content}\n"
elif role == "assistant":
conversation += f"Jarvis: {content}\n"
# Add model response
inputs = tokenizer(conversation, return_tensors="pt").to(device)
outputs = model.generate(**inputs, max_new_tokens=200, temperature=0.7)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Return only the assistant’s latest message (new format)
return {"role": "assistant", "content": response}
# --- Gradio Interface ---
demo = gr.ChatInterface(
fn=chat_with_jarvis,
title="Jarvis AI (Free CPU Edition)",
description="A lightweight Jarvis that runs 24/7 on Hugging Face CPU for free.",
type="messages", # <-- Required for new Gradio versions
theme="soft",
)
# Launch normally (Hugging Face handles hosting)
demo.launch()
|