|
from huggingface_hub import InferenceClient |
|
import gradio as gr |
|
|
|
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3") |
|
|
|
|
|
def format_prompt(message, history): |
|
prompt = "<s>" |
|
for user_prompt, bot_response in history: |
|
prompt += f"[INST] {user_prompt} [/INST]{bot_response}</s>" |
|
prompt += f"[INST] {message} [/INST]" |
|
return prompt |
|
|
|
|
|
def generate(message, history, file, temperature, max_new_tokens, top_p, repetition_penalty): |
|
if file: |
|
try: |
|
file_content = file.read().decode("utf-8") |
|
message += f"\n\n📎 Dosya içeriği:\n{file_content}" |
|
except Exception as e: |
|
message += f"\n\n📎 (Dosya okunamadı: {str(e)})" |
|
|
|
prompt = format_prompt(message, history) |
|
|
|
stream = client.text_generation( |
|
prompt, |
|
temperature=temperature, |
|
max_new_tokens=max_new_tokens, |
|
top_p=top_p, |
|
repetition_penalty=repetition_penalty, |
|
do_sample=True, |
|
seed=42, |
|
stream=True, |
|
details=True, |
|
return_full_text=False |
|
) |
|
|
|
output = "" |
|
for response in stream: |
|
output += response.token.text |
|
yield output |
|
|
|
|
|
def start_chat(approved): |
|
if approved: |
|
return gr.update(visible=False), gr.update(visible=True) |
|
else: |
|
raise gr.Error("Kutucuğu işaretlemeden devam edemezsin Patron!") |
|
|
|
|
|
with gr.Blocks(theme="Nymbo/Alyx_Theme") as app: |
|
|
|
with gr.Group(visible=True) as intro: |
|
gr.Markdown(""" |
|
## 📜 Kullanım Şartları ve Sorumluluk Reddi |
|
|
|
Bu uygulama deneysel bir yapay zekâ içerir. |
|
Tıbbi, hukuki, etik veya finansal tavsiye vermez. |
|
Tüm sorumluluk kullanıcıya aittir. |
|
|
|
Devam etmek için kutucuğu işaretleyin. |
|
""") |
|
agree = gr.Checkbox(label="✅ Okudum, Onaylıyorum") |
|
start_btn = gr.Button("🚀 Başla") |
|
|
|
|
|
with gr.Group(visible=False) as chat: |
|
chatbot = gr.Chatbot(label="🧠 AlpDroid", show_label=False) |
|
with gr.Row(): |
|
user_input = gr.Textbox(placeholder="Mesajını yaz...", scale=4) |
|
file_upload = gr.File(label="📎", file_types=[".txt", ".csv", ".md", ".json"], scale=1) |
|
send_btn = gr.Button("Gönder") |
|
|
|
|
|
with gr.Accordion("Ayarlar", open=False): |
|
temperature = gr.Slider(0.1, 1.0, value=0.9, label="🔥 Temperature") |
|
max_new_tokens = gr.Slider(64, 1024, value=256, step=64, label="🧠 Max New Tokens") |
|
top_p = gr.Slider(0.1, 1.0, value=0.95, label="🎯 Top-p") |
|
repetition_penalty = gr.Slider(1.0, 2.0, value=1.2, label="🔁 Repetition Penalty") |
|
|
|
|
|
state = gr.State([]) |
|
|
|
def user_submit(msg, history): |
|
history = history + [[msg, None]] |
|
return "", history |
|
|
|
def bot_response(history, file, temperature, max_new_tokens, top_p, repetition_penalty): |
|
message = history[-1][0] |
|
response = generate(message, history[:-1], file, temperature, max_new_tokens, top_p, repetition_penalty) |
|
return chatbot.stream(response, history, message_index=-1) |
|
|
|
send_btn.click(fn=user_submit, inputs=[user_input, state], outputs=[user_input, state]) |
|
send_btn.click(fn=bot_response, inputs=[state, file_upload, temperature, max_new_tokens, top_p, repetition_penalty], outputs=[chatbot, state]) |
|
|
|
|
|
start_btn.click(fn=start_chat, inputs=[agree], outputs=[intro, chat]) |
|
|
|
app.launch() |