Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline | |
| model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" | |
| tokenizer = AutoTokenizer.from_pretrained(model_id) | |
| model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", device_map="auto") | |
| pipe = pipeline("text-generation", model=model, tokenizer=tokenizer) | |
| def chat(message): | |
| messages = [ | |
| {"role": "system", "content": "You are a helpful assistant."}, | |
| {"role": "user", "content": message} | |
| ] | |
| prompt = f"<|user|>\n{message}\n<|assistant|>\n" | |
| output = pipe(prompt, max_new_tokens=200, do_sample=True, temperature=0.7)[0]["generated_text"] | |
| return output.split("<|assistant|>\n")[-1].strip() | |
| iface = gr.Interface(fn=chat, inputs="text", outputs="text", title="TinyLlama Chat") | |
| iface.launch() | |