Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,143 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
-
|
| 19 |
-
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
-
messages.extend(history)
|
| 22 |
-
|
| 23 |
-
messages.append({"role": "user", "content": message})
|
| 24 |
-
|
| 25 |
-
response = ""
|
| 26 |
-
|
| 27 |
-
for message in client.chat_completion(
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
type="messages",
|
| 49 |
-
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value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+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
from peft import PeftModel
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# --- Load tokenizer and model for CPU ---
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained("unsloth/Qwen2.5-0.5B-Instruct")
|
| 8 |
+
|
| 9 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
"unsloth/Qwen2.5-0.5B-Instruct",
|
| 11 |
+
dtype=torch.float32,
|
| 12 |
+
device_map={"": "cpu"},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
+
model = PeftModel.from_pretrained(base_model, "khazarai/SympQwen-0.5B").to("cpu")
|
| 16 |
+
|
| 17 |
+
# --- Chatbot logic ---
|
| 18 |
+
def generate_response(user_input, chat_history):
|
| 19 |
+
if not user_input.strip():
|
| 20 |
+
return chat_history, chat_history
|
| 21 |
+
|
| 22 |
+
chat_history.append({"role": "user", "content": user_input})
|
| 23 |
+
|
| 24 |
+
text = tokenizer.apply_chat_template(
|
| 25 |
+
chat_history,
|
| 26 |
+
tokenize=False,
|
| 27 |
+
add_generation_prompt=True,
|
| 28 |
+
#enable_thinking=False,
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
inputs = tokenizer(text, return_tensors="pt").to("cpu")
|
| 32 |
+
|
| 33 |
+
output_tokens = model.generate(
|
| 34 |
+
**inputs,
|
| 35 |
+
max_new_tokens=512,
|
| 36 |
+
#temperature=0.7,
|
| 37 |
+
#top_p=0.8,
|
| 38 |
+
#top_k=20,
|
| 39 |
+
do_sample=True
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
response = tokenizer.decode(output_tokens[0], skip_special_tokens=True)
|
| 43 |
+
response = response.split(user_input)[-1].strip()
|
| 44 |
+
|
| 45 |
+
chat_history.append({"role": "assistant", "content": response})
|
| 46 |
+
|
| 47 |
+
gr_chat_history = [
|
| 48 |
+
(m["content"], chat_history[i + 1]["content"])
|
| 49 |
+
for i, m in enumerate(chat_history[:-1])
|
| 50 |
+
if m["role"] == "user"
|
| 51 |
+
]
|
| 52 |
+
|
| 53 |
+
return gr_chat_history, chat_history
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# --- Advanced UI Design ---
|
| 57 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", secondary_hue="slate")) as demo:
|
| 58 |
+
gr.HTML("""
|
| 59 |
+
<style>
|
| 60 |
+
body {
|
| 61 |
+
background: radial-gradient(circle at top, #DBEAFE 0%, #EFF6FF 100%);
|
| 62 |
+
}
|
| 63 |
+
.gradio-container {
|
| 64 |
+
font-family: 'Inter', sans-serif;
|
| 65 |
+
}
|
| 66 |
+
.chat-header {
|
| 67 |
+
text-align: center;
|
| 68 |
+
background: linear-gradient(90deg, #60A5FA, #3B82F6);
|
| 69 |
+
color: white;
|
| 70 |
+
padding: 20px 10px;
|
| 71 |
+
border-radius: 18px;
|
| 72 |
+
margin-bottom: 20px;
|
| 73 |
+
box-shadow: 0px 4px 20px rgba(59,130,246,0.3);
|
| 74 |
+
}
|
| 75 |
+
.chat-header h1 {
|
| 76 |
+
font-size: 2.4em;
|
| 77 |
+
font-weight: 800;
|
| 78 |
+
margin-bottom: 0px;
|
| 79 |
+
}
|
| 80 |
+
.chat-header p {
|
| 81 |
+
margin-top: 5px;
|
| 82 |
+
color: #DBEAFE;
|
| 83 |
+
font-weight: 500;
|
| 84 |
+
}
|
| 85 |
+
.send-btn {
|
| 86 |
+
background: linear-gradient(90deg, #60A5FA, #3B82F6);
|
| 87 |
+
color: white !important;
|
| 88 |
+
transition: all 0.25s ease-in-out;
|
| 89 |
+
}
|
| 90 |
+
.send-btn:hover {
|
| 91 |
+
transform: scale(1.05);
|
| 92 |
+
box-shadow: 0 0 12px rgba(96,165,250,0.5);
|
| 93 |
+
}
|
| 94 |
+
.textbox {
|
| 95 |
+
backdrop-filter: blur(12px);
|
| 96 |
+
background-color: rgba(255,255,255,0.6);
|
| 97 |
+
border-radius: 16px !important;
|
| 98 |
+
}
|
| 99 |
+
.footer {
|
| 100 |
+
text-align: center;
|
| 101 |
+
margin-top: 25px;
|
| 102 |
+
color: #6B7280;
|
| 103 |
+
font-size: 0.9em;
|
| 104 |
+
}
|
| 105 |
+
</style>
|
| 106 |
+
|
| 107 |
+
<div class="chat-header">
|
| 108 |
+
<h1> π§ Azerbaijani Chatbot</h1>
|
| 109 |
+
</div>
|
| 110 |
+
""")
|
| 111 |
+
|
| 112 |
+
with gr.Row():
|
| 113 |
+
with gr.Column(scale=6):
|
| 114 |
+
chatbot = gr.Chatbot(
|
| 115 |
+
label="π¬ Chat-Az",
|
| 116 |
+
height=600,
|
| 117 |
+
bubble_full_width=True,
|
| 118 |
+
show_copy_button=True,
|
| 119 |
+
avatar_images=(
|
| 120 |
+
"https://cdn-icons-png.flaticon.com/512/1077/1077012.png", # user
|
| 121 |
+
"https://cdn-icons-png.flaticon.com/512/4140/4140048.png", # bot
|
| 122 |
+
),
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
user_input = gr.Textbox(
|
| 126 |
+
placeholder="Ask about..",
|
| 127 |
+
label="Type your question",
|
| 128 |
+
lines=3,
|
| 129 |
+
elem_classes=["textbox"],
|
| 130 |
+
autofocus=True,
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
with gr.Row():
|
| 134 |
+
send_btn = gr.Button("π Send", variant="primary", elem_classes=["send-btn"])
|
| 135 |
+
clear_btn = gr.Button("π§Ή Clear Chat")
|
| 136 |
+
|
| 137 |
+
state = gr.State([])
|
| 138 |
|
| 139 |
+
send_btn.click(generate_response, [user_input, state], [chatbot, state])
|
| 140 |
+
user_input.submit(generate_response, [user_input, state], [chatbot, state])
|
| 141 |
+
clear_btn.click(lambda: ([], []), None, [chatbot, state])
|
| 142 |
|
| 143 |
+
demo.launch(share=True)
|
|
|