Spaces:
Sleeping
Sleeping
Update space
Browse files- app.py +124 -0
- requirements.txt +5 -0
- style.css +121 -0
app.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 3 |
+
from threading import Thread
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
model_name = "Hhsjsnns/PyCoder-QLoRA-v1"
|
| 7 |
+
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, dtype='auto', device_map='auto')
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 11 |
+
|
| 12 |
+
def PyCoder(message, history):
|
| 13 |
+
|
| 14 |
+
messages = [
|
| 15 |
+
{'role': 'system', 'content': "You are a PyCoder, an expert Python & AI assistant."}
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def extract_text(content):
|
| 20 |
+
if isinstance(content, list):
|
| 21 |
+
return " ".join(
|
| 22 |
+
block.get("text", "") for block in content if isinstance(block, dict)
|
| 23 |
+
)
|
| 24 |
+
return str(content)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
for msg in history:
|
| 28 |
+
role = msg.get("role")
|
| 29 |
+
content = extract_text(msg.get("content"))
|
| 30 |
+
|
| 31 |
+
if role in ("user", "assistant") and content.strip():
|
| 32 |
+
messages.append({
|
| 33 |
+
"role": role,
|
| 34 |
+
"content": content
|
| 35 |
+
})
|
| 36 |
+
messages.append({"role": "user", "content": message})
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
prompt = tokenizer.apply_chat_template(
|
| 41 |
+
messages,
|
| 42 |
+
tokenize = False,
|
| 43 |
+
add_generation_prompt = True
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
streamer = TextIteratorStreamer(
|
| 47 |
+
tokenizer,
|
| 48 |
+
skip_prompt=True,
|
| 49 |
+
skip_special_tokens = True
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
inputs = tokenizer(prompt, return_tensors='pt').to(model.device)
|
| 53 |
+
|
| 54 |
+
generation_args = {
|
| 55 |
+
'max_new_tokens': 300,
|
| 56 |
+
'streamer': streamer,
|
| 57 |
+
**inputs
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
thread = Thread(
|
| 61 |
+
target = model.generate,
|
| 62 |
+
kwargs=generation_args,
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
thread.start()
|
| 66 |
+
|
| 67 |
+
acc_text = ""
|
| 68 |
+
|
| 69 |
+
for token in streamer:
|
| 70 |
+
acc_text += token
|
| 71 |
+
yield acc_text
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# Ensure the generation thread completes
|
| 76 |
+
thread.join()
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
#---------------------UI---------------------------------
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
with open("style.css", "r", encoding="utf-8") as f:
|
| 86 |
+
css = f.read()
|
| 87 |
+
|
| 88 |
+
with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
|
| 89 |
+
|
| 90 |
+
gr.HTML("""
|
| 91 |
+
<div class="app-header">
|
| 92 |
+
<span class="logo-glow">PyCoder</span>
|
| 93 |
+
</div>
|
| 94 |
+
""")
|
| 95 |
+
|
| 96 |
+
with gr.Column(elem_classes="chatbot"):
|
| 97 |
+
gr.ChatInterface(fn=PyCoder)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
with gr.Sidebar():
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
gr.HTML("""
|
| 107 |
+
<div class="settings-card">
|
| 108 |
+
<div class="settings-title">⚙️ Settings</div>
|
| 109 |
+
|
| 110 |
+
<div class="settings-item">
|
| 111 |
+
<span>Model</span>
|
| 112 |
+
<span class="settings-badge">PyCoder-QLoRA-v1</span>
|
| 113 |
+
</div>
|
| 114 |
+
|
| 115 |
+
<div class="settings-item">
|
| 116 |
+
<span>Max Tokens</span>
|
| 117 |
+
<span class="settings-badge">300</span>
|
| 118 |
+
</div>
|
| 119 |
+
</div>
|
| 120 |
+
""")
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
tokenizers
|
| 4 |
+
gradio
|
| 5 |
+
|
style.css
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* 🌌 Global background */
|
| 2 |
+
body {
|
| 3 |
+
background: radial-gradient(
|
| 4 |
+
1200px circle at top,
|
| 5 |
+
#1b1f3b,
|
| 6 |
+
#0f1225 60%
|
| 7 |
+
);
|
| 8 |
+
color: #e8ebff;
|
| 9 |
+
font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
/* 🧭 Header */
|
| 13 |
+
.app-header {
|
| 14 |
+
position: sticky;
|
| 15 |
+
top: 0;
|
| 16 |
+
z-index: 50;
|
| 17 |
+
display: flex;
|
| 18 |
+
align-items: center;
|
| 19 |
+
justify-content: center;
|
| 20 |
+
height: 64px;
|
| 21 |
+
margin-bottom: 14px;
|
| 22 |
+
|
| 23 |
+
background: rgba(20, 22, 45, 0.65);
|
| 24 |
+
backdrop-filter: blur(22px);
|
| 25 |
+
-webkit-backdrop-filter: blur(22px);
|
| 26 |
+
|
| 27 |
+
border-bottom: 1px solid rgba(255,255,255,0.08);
|
| 28 |
+
box-shadow:
|
| 29 |
+
0 8px 30px rgba(0,0,0,0.45),
|
| 30 |
+
inset 0 1px 0 rgba(255,255,255,0.05);
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
/* ✨ Logo Text */
|
| 34 |
+
.logo-glow {
|
| 35 |
+
font-size: 26px;
|
| 36 |
+
font-weight: 700;
|
| 37 |
+
letter-spacing: 1px;
|
| 38 |
+
|
| 39 |
+
background: linear-gradient(
|
| 40 |
+
135deg,
|
| 41 |
+
#a78bfa,
|
| 42 |
+
#22d3ee
|
| 43 |
+
);
|
| 44 |
+
-webkit-background-clip: text;
|
| 45 |
+
-webkit-text-fill-color: transparent;
|
| 46 |
+
|
| 47 |
+
text-shadow:
|
| 48 |
+
0 0 14px rgba(167,139,250,0.35),
|
| 49 |
+
0 0 26px rgba(34,211,238,0.2);
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
/* 🔥 Hover pulse */
|
| 53 |
+
.logo-glow:hover {
|
| 54 |
+
text-shadow:
|
| 55 |
+
0 0 20px rgba(167,139,250,0.6),
|
| 56 |
+
0 0 36px rgba(34,211,238,0.35);
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
/* 💬 Chat container */
|
| 60 |
+
.chatbot {
|
| 61 |
+
height: 75vh !important;
|
| 62 |
+
max-height: 75vh !important;
|
| 63 |
+
|
| 64 |
+
max-width: 92vw;
|
| 65 |
+
margin: 0 auto 20px auto;
|
| 66 |
+
|
| 67 |
+
background: rgba(255,255,255,0.02);
|
| 68 |
+
border-radius: 20px;
|
| 69 |
+
box-shadow: 0 10px 40px rgba(0,0,0,0.4);
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
/* ⚙️ Settings Card */
|
| 73 |
+
.settings-card {
|
| 74 |
+
background: rgba(255,255,255,0.07);
|
| 75 |
+
border-radius: 20px;
|
| 76 |
+
padding: 18px;
|
| 77 |
+
margin-bottom: 14px;
|
| 78 |
+
|
| 79 |
+
backdrop-filter: blur(18px);
|
| 80 |
+
border: 1px solid rgba(255,255,255,0.14);
|
| 81 |
+
|
| 82 |
+
box-shadow:
|
| 83 |
+
0 10px 32px rgba(0,0,0,0.45),
|
| 84 |
+
inset 0 1px 0 rgba(255,255,255,0.05);
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
/* ⚙️ Settings title */
|
| 88 |
+
.settings-title {
|
| 89 |
+
font-size: 15px;
|
| 90 |
+
font-weight: 600;
|
| 91 |
+
margin-bottom: 14px;
|
| 92 |
+
color: #c7d2fe;
|
| 93 |
+
letter-spacing: 0.5px;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
/* ⚙️ Setting rows */
|
| 97 |
+
.settings-item {
|
| 98 |
+
display: flex;
|
| 99 |
+
justify-content: space-between;
|
| 100 |
+
align-items: center;
|
| 101 |
+
padding: 9px 0;
|
| 102 |
+
font-size: 14px;
|
| 103 |
+
color: #e0e7ff;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
/* 🏷️ Badges */
|
| 107 |
+
.settings-badge {
|
| 108 |
+
background: linear-gradient(
|
| 109 |
+
135deg,
|
| 110 |
+
rgba(167,139,250,0.25),
|
| 111 |
+
rgba(34,211,238,0.25)
|
| 112 |
+
);
|
| 113 |
+
padding: 5px 12px;
|
| 114 |
+
border-radius: 999px;
|
| 115 |
+
font-size: 12px;
|
| 116 |
+
font-weight: 600;
|
| 117 |
+
color: #ffffff;
|
| 118 |
+
|
| 119 |
+
box-shadow:
|
| 120 |
+
0 4px 14px rgba(34,211,238,0.35);
|
| 121 |
+
}
|