Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
from huggingface_hub import InferenceClient
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
# HuggingFace Mistral modeli istemcisi
|
| 5 |
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
|
| 6 |
|
| 7 |
-
#
|
| 8 |
def format_prompt(message, history):
|
| 9 |
prompt = "<s>"
|
| 10 |
for user_prompt, bot_response in history:
|
|
@@ -12,23 +11,19 @@ def format_prompt(message, history):
|
|
| 12 |
prompt += f"[INST] {message} [/INST]"
|
| 13 |
return prompt
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
def generate(message, history, file
|
| 17 |
-
|
| 18 |
-
if file is not None:
|
| 19 |
try:
|
| 20 |
file_content = file.read().decode("utf-8")
|
| 21 |
-
message += f"\n\n📎
|
| 22 |
except Exception as e:
|
| 23 |
-
message += f"\n\n(Dosya okunamadı: {str(e)})"
|
| 24 |
|
| 25 |
-
|
| 26 |
-
temperature = float(max(temperature, 1e-2))
|
| 27 |
-
top_p = float(top_p)
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
generate_kwargs = dict(
|
| 32 |
temperature=temperature,
|
| 33 |
max_new_tokens=max_new_tokens,
|
| 34 |
top_p=top_p,
|
|
@@ -37,69 +32,68 @@ def generate(message, history, file=None, temperature=0.9, max_new_tokens=256, t
|
|
| 37 |
seed=42,
|
| 38 |
stream=True,
|
| 39 |
details=True,
|
| 40 |
-
return_full_text=False
|
| 41 |
)
|
| 42 |
|
| 43 |
-
stream = client.text_generation(formatted_prompt, **generate_kwargs)
|
| 44 |
output = ""
|
| 45 |
for response in stream:
|
| 46 |
output += response.token.text
|
| 47 |
yield output
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
gr.Slider(label="🔥 Temperature", value=0.9, minimum=0.0, maximum=1.0, step=0.05),
|
| 53 |
-
gr.Slider(label="🧠 Max new tokens", value=256, minimum=0, maximum=1024, step=64),
|
| 54 |
-
gr.Slider(label="🎯 Top-p", value=0.9, minimum=0.0, maximum=1.0, step=0.05),
|
| 55 |
-
gr.Slider(label="🔁 Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05),
|
| 56 |
-
]
|
| 57 |
-
|
| 58 |
-
# Giriş ekranındaki butona basıldığında
|
| 59 |
-
def start_chat(checkbox_state):
|
| 60 |
-
if checkbox_state:
|
| 61 |
return gr.update(visible=False), gr.update(visible=True)
|
| 62 |
else:
|
| 63 |
-
raise gr.Error("
|
| 64 |
|
| 65 |
-
#
|
| 66 |
with gr.Blocks(theme="Nymbo/Alyx_Theme") as app:
|
| 67 |
-
# Giriş ekranı
|
| 68 |
-
with gr.Group(visible=True) as
|
| 69 |
gr.Markdown("""
|
| 70 |
-
##
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
- Tüm kullanım **kendi sorumluluğunuzdadır**.
|
| 76 |
|
| 77 |
-
Devam etmek için
|
| 78 |
""")
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
#
|
| 83 |
-
with gr.Group(visible=False) as
|
| 84 |
-
gr.
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
# Giriş ekranından chate geçiş
|
| 102 |
-
|
| 103 |
|
| 104 |
-
# Uygulamayı başlat
|
| 105 |
app.launch()
|
|
|
|
| 1 |
from huggingface_hub import InferenceClient
|
| 2 |
import gradio as gr
|
| 3 |
|
|
|
|
| 4 |
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
|
| 5 |
|
| 6 |
+
# Prompt formatlayıcı
|
| 7 |
def format_prompt(message, history):
|
| 8 |
prompt = "<s>"
|
| 9 |
for user_prompt, bot_response in history:
|
|
|
|
| 11 |
prompt += f"[INST] {message} [/INST]"
|
| 12 |
return prompt
|
| 13 |
|
| 14 |
+
# Modelden cevap üret
|
| 15 |
+
def generate(message, history, file, temperature, max_new_tokens, top_p, repetition_penalty):
|
| 16 |
+
if file:
|
|
|
|
| 17 |
try:
|
| 18 |
file_content = file.read().decode("utf-8")
|
| 19 |
+
message += f"\n\n📎 Dosya içeriği:\n{file_content}"
|
| 20 |
except Exception as e:
|
| 21 |
+
message += f"\n\n📎 (Dosya okunamadı: {str(e)})"
|
| 22 |
|
| 23 |
+
prompt = format_prompt(message, history)
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
stream = client.text_generation(
|
| 26 |
+
prompt,
|
|
|
|
| 27 |
temperature=temperature,
|
| 28 |
max_new_tokens=max_new_tokens,
|
| 29 |
top_p=top_p,
|
|
|
|
| 32 |
seed=42,
|
| 33 |
stream=True,
|
| 34 |
details=True,
|
| 35 |
+
return_full_text=False
|
| 36 |
)
|
| 37 |
|
|
|
|
| 38 |
output = ""
|
| 39 |
for response in stream:
|
| 40 |
output += response.token.text
|
| 41 |
yield output
|
| 42 |
+
|
| 43 |
+
# Kullanıcı onayı kontrolü
|
| 44 |
+
def start_chat(approved):
|
| 45 |
+
if approved:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
return gr.update(visible=False), gr.update(visible=True)
|
| 47 |
else:
|
| 48 |
+
raise gr.Error("Kutucuğu işaretlemeden devam edemezsin Patron!")
|
| 49 |
|
| 50 |
+
# Arayüz
|
| 51 |
with gr.Blocks(theme="Nymbo/Alyx_Theme") as app:
|
| 52 |
+
# Giriş ekranı
|
| 53 |
+
with gr.Group(visible=True) as intro:
|
| 54 |
gr.Markdown("""
|
| 55 |
+
## 📜 Kullanım Şartları ve Sorumluluk Reddi
|
| 56 |
|
| 57 |
+
Bu uygulama deneysel bir yapay zekâ içerir.
|
| 58 |
+
Tıbbi, hukuki, etik veya finansal tavsiye vermez.
|
| 59 |
+
Tüm sorumluluk kullanıcıya aittir.
|
|
|
|
| 60 |
|
| 61 |
+
Devam etmek için kutucuğu işaretleyin.
|
| 62 |
""")
|
| 63 |
+
agree = gr.Checkbox(label="✅ Okudum, Onaylıyorum")
|
| 64 |
+
start_btn = gr.Button("🚀 Başla")
|
| 65 |
+
|
| 66 |
+
# Chat ekranı
|
| 67 |
+
with gr.Group(visible=False) as chat:
|
| 68 |
+
chatbot = gr.Chatbot(label="🧠 AlpDroid", show_label=False)
|
| 69 |
+
with gr.Row():
|
| 70 |
+
user_input = gr.Textbox(placeholder="Mesajını yaz...", scale=4)
|
| 71 |
+
file_upload = gr.File(label="📎", file_types=[".txt", ".csv", ".md", ".json"], scale=1)
|
| 72 |
+
send_btn = gr.Button("Gönder")
|
| 73 |
+
|
| 74 |
+
# Ayarlar
|
| 75 |
+
with gr.Accordion("Ayarlar", open=False):
|
| 76 |
+
temperature = gr.Slider(0.1, 1.0, value=0.9, label="🔥 Temperature")
|
| 77 |
+
max_new_tokens = gr.Slider(64, 1024, value=256, step=64, label="🧠 Max New Tokens")
|
| 78 |
+
top_p = gr.Slider(0.1, 1.0, value=0.95, label="🎯 Top-p")
|
| 79 |
+
repetition_penalty = gr.Slider(1.0, 2.0, value=1.2, label="🔁 Repetition Penalty")
|
| 80 |
+
|
| 81 |
+
# Geçmiş
|
| 82 |
+
state = gr.State([])
|
| 83 |
+
|
| 84 |
+
def user_submit(msg, history):
|
| 85 |
+
history = history + [[msg, None]]
|
| 86 |
+
return "", history
|
| 87 |
+
|
| 88 |
+
def bot_response(history, file, temperature, max_new_tokens, top_p, repetition_penalty):
|
| 89 |
+
message = history[-1][0]
|
| 90 |
+
response = generate(message, history[:-1], file, temperature, max_new_tokens, top_p, repetition_penalty)
|
| 91 |
+
return chatbot.stream(response, history, message_index=-1)
|
| 92 |
+
|
| 93 |
+
send_btn.click(fn=user_submit, inputs=[user_input, state], outputs=[user_input, state])
|
| 94 |
+
send_btn.click(fn=bot_response, inputs=[state, file_upload, temperature, max_new_tokens, top_p, repetition_penalty], outputs=[chatbot, state])
|
| 95 |
|
| 96 |
# Giriş ekranından chate geçiş
|
| 97 |
+
start_btn.click(fn=start_chat, inputs=[agree], outputs=[intro, chat])
|
| 98 |
|
|
|
|
| 99 |
app.launch()
|