Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
model_name = "mathanmithun/dhigrowth_qwen2p5_0_5b_merged_model"
|
| 6 |
+
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_name,
|
| 10 |
+
torch_dtype=torch.float32, # required for CPU hosting
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def chat(message, history):
|
| 14 |
+
# Build conversation history text
|
| 15 |
+
conversation = ""
|
| 16 |
+
for usr, bot in history:
|
| 17 |
+
conversation += f"User: {usr}\nAssistant: {bot}\n"
|
| 18 |
+
|
| 19 |
+
prompt = conversation + f"User: {message}\nAssistant:"
|
| 20 |
+
|
| 21 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 22 |
+
output = model.generate(
|
| 23 |
+
**inputs,
|
| 24 |
+
max_new_tokens=200,
|
| 25 |
+
temperature=0.8,
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
reply = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 29 |
+
reply = reply.split("Assistant:")[-1].strip()
|
| 30 |
+
|
| 31 |
+
history.append((message, reply))
|
| 32 |
+
return reply, history
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# UI
|
| 36 |
+
with gr.Blocks() as demo:
|
| 37 |
+
gr.Markdown("### 🤖 DhiGrowth AI Chatbot")
|
| 38 |
+
chatbot = gr.Chatbot()
|
| 39 |
+
msg = gr.Textbox(placeholder="Ask me anything...")
|
| 40 |
+
|
| 41 |
+
def user_input(text, history):
|
| 42 |
+
return "", history + [[text, None]]
|
| 43 |
+
|
| 44 |
+
msg.submit(user_input, [msg, chatbot], [msg, chatbot]).then(
|
| 45 |
+
chat, [msg, chatbot], [chatbot, chatbot]
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
demo.launch()
|