Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +90 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 3 |
+
|
| 4 |
+
# Model ve tokenizer yüklemeleri için quantize ayarları
|
| 5 |
+
bnb_config = BitsAndBytesConfig(
|
| 6 |
+
load_in_8bit=True,
|
| 7 |
+
llm_int8_threshold=6.0
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def load_model():
|
| 12 |
+
try:
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 14 |
+
"deepseek-ai/deepseek-r1",
|
| 15 |
+
trust_remote_code=True
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
+
"deepseek-ai/deepseek-r1",
|
| 20 |
+
trust_remote_code=True,
|
| 21 |
+
quantization_config=bnb_config,
|
| 22 |
+
device_map="auto",
|
| 23 |
+
low_cpu_mem_usage=True
|
| 24 |
+
)
|
| 25 |
+
return tokenizer, model
|
| 26 |
+
except Exception as e:
|
| 27 |
+
st.error(f"Model yüklenemedi: {str(e)}")
|
| 28 |
+
st.stop()
|
| 29 |
+
|
| 30 |
+
tokenizer, model = load_model()
|
| 31 |
+
|
| 32 |
+
# Streamlit arayüzü
|
| 33 |
+
st.title("DeepSeek-R1 ChatBot")
|
| 34 |
+
st.markdown("""
|
| 35 |
+
**Not:** Bu demo modeli CPU üzerinde çalışmaktadır. Yanıt süreleri GPU'ya göre daha uzun olabilir.
|
| 36 |
+
""")
|
| 37 |
+
|
| 38 |
+
# Sohbet geçmişi yönetimi
|
| 39 |
+
if "messages" not in st.session_state:
|
| 40 |
+
st.session_state.messages = []
|
| 41 |
+
|
| 42 |
+
# Geçmiş mesajları göster
|
| 43 |
+
for message in st.session_state.messages:
|
| 44 |
+
with st.chat_message(message["role"]):
|
| 45 |
+
st.markdown(message["content"])
|
| 46 |
+
|
| 47 |
+
# Kullanıcı girişi
|
| 48 |
+
prompt = st.chat_input("Mesajınızı yazın...")
|
| 49 |
+
|
| 50 |
+
if prompt:
|
| 51 |
+
# Kullanıcı mesajını ekle
|
| 52 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 53 |
+
with st.chat_message("user"):
|
| 54 |
+
st.markdown(prompt)
|
| 55 |
+
|
| 56 |
+
# Asistan yanıtı oluştur
|
| 57 |
+
with st.chat_message("assistant"):
|
| 58 |
+
message_placeholder = st.empty()
|
| 59 |
+
full_response = ""
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
# Model için prompt formatı
|
| 63 |
+
inputs = tokenizer.apply_chat_template(
|
| 64 |
+
[{"role": "user", "content": prompt}],
|
| 65 |
+
return_tensors="pt"
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# Generation ayarları
|
| 69 |
+
outputs = model.generate(
|
| 70 |
+
inputs.to(model.device),
|
| 71 |
+
max_new_tokens=512,
|
| 72 |
+
temperature=0.7,
|
| 73 |
+
top_p=0.9,
|
| 74 |
+
do_sample=True,
|
| 75 |
+
pad_token_id=tokenizer.eos_token_id
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
full_response = tokenizer.decode(
|
| 79 |
+
outputs[0][len(inputs[0]):],
|
| 80 |
+
skip_special_tokens=True
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
full_response = f"Hata oluştu: {str(e)}"
|
| 85 |
+
|
| 86 |
+
message_placeholder.markdown(full_response)
|
| 87 |
+
|
| 88 |
+
st.session_state.messages.append(
|
| 89 |
+
{"role": "assistant", "content": full_response}
|
| 90 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.32.0
|
| 2 |
+
transformers==4.38.2
|
| 3 |
+
torch==2.0.1
|
| 4 |
+
accelerate==0.27.2
|
| 5 |
+
bitsandbytes==0.42.0
|
| 6 |
+
numpy==1.26.4
|