Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load Zephyr model
|
| 6 |
+
model_id = "HuggingFaceH4/zephyr-7b-beta"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
|
| 9 |
+
|
| 10 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 11 |
+
|
| 12 |
+
# Chat function
|
| 13 |
+
def chat_with_bot(user_input):
|
| 14 |
+
system_message = "You are a helpful, honest, and friendly assistant."
|
| 15 |
+
prompt = f"<|system|>\n{system_message}\n<|user|>\n{user_input}\n<|assistant|>\n"
|
| 16 |
+
response = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_p=0.9)[0]["generated_text"]
|
| 17 |
+
answer = response.split("<|assistant|>")[-1].strip()
|
| 18 |
+
return answer
|
| 19 |
+
|
| 20 |
+
# Gradio interface
|
| 21 |
+
iface = gr.Interface(fn=chat_with_bot,
|
| 22 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask me anything..."),
|
| 23 |
+
outputs="text",
|
| 24 |
+
title="Zephyr Chatbot",
|
| 25 |
+
description="Ask general questions and get helpful answers!")
|
| 26 |
+
|
| 27 |
+
iface.launch()
|