Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,63 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Load
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
def chat(user_input, max_new_tokens=
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
max_new_tokens=max_new_tokens,
|
| 16 |
temperature=temperature,
|
| 17 |
top_p=0.95,
|
| 18 |
top_k=64,
|
| 19 |
do_sample=True,
|
| 20 |
-
eos_token_id=tokenizer.eos_token_id
|
| 21 |
)
|
| 22 |
-
return tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
with gr.Blocks() as demo:
|
| 26 |
-
gr.Markdown("#
|
|
|
|
| 27 |
chatbot = gr.Chatbot()
|
| 28 |
-
user_input = gr.Textbox(label="
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
def respond(
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
MODEL_NAME = "Devishetty100/savyasachi"
|
| 6 |
+
|
| 7 |
+
# Load tokenizer
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 9 |
|
| 10 |
+
# Load model (SAFE for Spaces)
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
MODEL_NAME,
|
| 13 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 14 |
+
device_map="auto",
|
| 15 |
+
)
|
| 16 |
|
| 17 |
+
# Chat function
|
| 18 |
+
def chat(user_input, history, max_new_tokens=200, temperature=1.0):
|
| 19 |
+
messages = []
|
| 20 |
+
|
| 21 |
+
for user, assistant in history:
|
| 22 |
+
messages.append({"role": "user", "content": user})
|
| 23 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 24 |
+
|
| 25 |
+
messages.append({"role": "user", "content": user_input})
|
| 26 |
+
|
| 27 |
+
prompt = tokenizer.apply_chat_template(
|
| 28 |
+
messages,
|
| 29 |
+
tokenize=False,
|
| 30 |
+
add_generation_prompt=True,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 34 |
+
|
| 35 |
+
outputs = model.generate(
|
| 36 |
+
**inputs,
|
| 37 |
max_new_tokens=max_new_tokens,
|
| 38 |
temperature=temperature,
|
| 39 |
top_p=0.95,
|
| 40 |
top_k=64,
|
| 41 |
do_sample=True,
|
| 42 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 43 |
)
|
|
|
|
| 44 |
|
| 45 |
+
response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
|
| 46 |
+
return response
|
| 47 |
+
|
| 48 |
+
# Gradio UI
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("## 🕉️ Savyasachi — Devotee of Lord Krishna")
|
| 51 |
+
|
| 52 |
chatbot = gr.Chatbot()
|
| 53 |
+
user_input = gr.Textbox(label="Ask Krishna")
|
| 54 |
+
send = gr.Button("Send")
|
| 55 |
+
|
| 56 |
+
def respond(message, history):
|
| 57 |
+
reply = chat(message, history)
|
| 58 |
+
history.append((message, reply))
|
| 59 |
+
return history, ""
|
| 60 |
+
|
| 61 |
+
send.click(respond, [user_input, chatbot], [chatbot, user_input])
|
| 62 |
+
|
| 63 |
demo.launch()
|