alperall's picture
Update app.py
4f5b2da verified
raw
history blame
3.45 kB
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=None, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.2):
temperature = float(max(temperature, 1e-2))
top_p = float(top_p)
# Eğer kullanıcı dosya eklediyse, dosya içeriğini mesaja ekle
if file is not None:
try:
file_content = file.read().decode("utf-8")
message = f"{message}\n\nEkli dosyanın içeriği:\n{file_content}"
except Exception as e:
message = f"{message}\n\n(Dosyayı okuyamadım: {str(e)})"
formatted_prompt = format_prompt(message, history)
stream = client.text_generation(
formatted_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
return output
# Ayarlar (slider'lar)
additional_inputs = [
gr.Slider(label="Temperature", value=0.9, minimum=0.0, maximum=1.0, step=0.05),
gr.Slider(label="Max new tokens", value=256, minimum=0, maximum=1024, step=64),
gr.Slider(label="Top-p (nucleus sampling)", value=0.9, minimum=0.0, maximum=1.0, step=0.05),
gr.Slider(label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05),
]
# Giriş ekranı
def start_chat(checkbox_state):
if checkbox_state:
return gr.update(visible=False), gr.update(visible=True)
else:
raise gr.Error("Devam etmek için kutucuğu işaretlemeniz gerekiyor!")
# Arayüz başlatma
with gr.Blocks(theme="Nymbo/Alyx_Theme") as app:
# Giriş sayfası
with gr.Group(visible=True) as intro_page:
gr.Markdown("""
## ❗ Kullanım Şartları ve Sorumluluk Reddi
**AlpDroid**, ALPERALL tarafından geliştirilen deneysel bir yapay zeka karakteridir.
- Yapay zekadır, tavsiyeleri bağlayıcı değildir
- Bilgi eğlence amaçlıdır
- Sorumluluk kullanıcıya aittir
Devam etmek için aşağıdaki kutucuğu işaretle.
""")
checkbox = gr.Checkbox(label="✅ Okudum, Onaylıyorum")
btn = gr.Button("🚀 Devam Et")
# Chat ekranı
with gr.Group(visible=False) as chat_page:
gr.ChatInterface(
fn=generate,
chatbot=gr.Chatbot(
show_label=False,
show_share_button=False,
show_copy_button=True,
likeable=True,
layout="panel"
),
additional_inputs=additional_inputs,
title="📎 AlpDroid Mistral Chat",
description="Ataç ile dosya gönder, konuşmaya dahil et.",
inputs=[gr.Textbox(placeholder="Mesajını yaz..."), gr.File(label="📎 Dosya yükle", file_types=[".txt", ".md", ".csv", ".json"])],
)
# Geçiş
btn.click(fn=start_chat, inputs=[checkbox], outputs=[intro_page, chat_page])
app.launch()