unnati14's picture
Upload 2 files
7f1c69e verified
Raw History Blame Contribute Delete
1.3 kB
# -*- coding: utf-8 -*-
"""Untitled1.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1Jr9keTYjS-y-cIMCjH-ygRzRhPOad0Z3
"""
# app.py
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import gradio as gr
model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype="auto")
chat_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
def chat_with_model(message, history):
prompt = f"### User:\n{message}\n### Assistant:\n"
response = chat_pipeline(prompt, max_new_tokens=200, do_sample=True, temperature=0.7)[0]["generated_text"]
reply = response.split("### Assistant:")[-1].strip()
history.append((f"You: {message}", f"Bot: {reply}"))
return history, history
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🤖 TinyLLaMA Chatbot\nAsk anything, anytime!\nPowered by Hugging Face")
chatbot = gr.Chatbot(label="Chat History")
msg = gr.Textbox(label="Enter your message", placeholder="E.g. What is AI?", lines=1)
state = gr.State([])
msg.submit(chat_with_model, [msg, state], [chatbot, state])
demo.launch()