|
|
import gradio as gr |
|
|
import os |
|
|
from huggingface_hub import login |
|
|
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM |
|
|
from langchain_community.llms import HuggingFacePipeline |
|
|
from langchain.chains import LLMChain |
|
|
from langchain.prompts import PromptTemplate |
|
|
|
|
|
|
|
|
model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" |
|
|
login(token=os.environ["HUGGINGFACE_TOKEN"]) |
|
|
|
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model_id) |
|
|
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype="auto") |
|
|
|
|
|
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=256) |
|
|
|
|
|
llm = HuggingFacePipeline(pipeline=pipe) |
|
|
|
|
|
|
|
|
prompt = PromptTemplate.from_template("You are Krish, a wise and witty friend.\n\nUser: {question}\nKrish:") |
|
|
|
|
|
chain = LLMChain(prompt=prompt, llm=llm) |
|
|
|
|
|
|
|
|
def chat_fn(message): |
|
|
response = chain.run({"question": message}) |
|
|
return response.strip() |
|
|
|
|
|
iface = gr.Interface(fn=chat_fn, inputs="text", outputs="text", title="π¦ Meet Krish", description="A wise, witty, and compassionate friend - KrishWay") |
|
|
|
|
|
iface.launch() |
|
|
|