alperall commited on
Commit
db6d3b4
·
verified ·
1 Parent(s): 4f5b2da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -30
app.py CHANGED
@@ -1,8 +1,10 @@
1
  from huggingface_hub import InferenceClient
2
  import gradio as gr
3
 
 
4
  client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
5
 
 
6
  def format_prompt(message, history):
7
  prompt = "<s>"
8
  for user_prompt, bot_response in history:
@@ -10,22 +12,23 @@ def format_prompt(message, history):
10
  prompt += f"[INST] {message} [/INST]"
11
  return prompt
12
 
 
13
  def generate(message, history, file=None, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.2):
14
- temperature = float(max(temperature, 1e-2))
15
- top_p = float(top_p)
16
-
17
- # Eğer kullanıcı dosya eklediyse, dosya içeriğini mesaja ekle
18
  if file is not None:
19
  try:
20
  file_content = file.read().decode("utf-8")
21
- message = f"{message}\n\nEkli dosyanın içeriği:\n{file_content}"
22
  except Exception as e:
23
- message = f"{message}\n\n(Dosyayı okuyamadım: {str(e)})"
 
 
 
 
24
 
25
  formatted_prompt = format_prompt(message, history)
26
 
27
- stream = client.text_generation(
28
- formatted_prompt,
29
  temperature=temperature,
30
  max_new_tokens=max_new_tokens,
31
  top_p=top_p,
@@ -37,45 +40,46 @@ def generate(message, history, file=None, temperature=0.9, max_new_tokens=256, t
37
  return_full_text=False,
38
  )
39
 
 
40
  output = ""
41
  for response in stream:
42
  output += response.token.text
43
  yield output
44
  return output
45
 
46
- # Ayarlar (slider'lar)
47
  additional_inputs = [
48
- gr.Slider(label="Temperature", value=0.9, minimum=0.0, maximum=1.0, step=0.05),
49
- gr.Slider(label="Max new tokens", value=256, minimum=0, maximum=1024, step=64),
50
- gr.Slider(label="Top-p (nucleus sampling)", value=0.9, minimum=0.0, maximum=1.0, step=0.05),
51
- gr.Slider(label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05),
52
  ]
53
 
54
- # Giriş ekranı
55
  def start_chat(checkbox_state):
56
  if checkbox_state:
57
  return gr.update(visible=False), gr.update(visible=True)
58
  else:
59
- raise gr.Error("Devam etmek için kutucuğu işaretlemeniz gerekiyor!")
60
 
61
- # Arayüz başlatma
62
  with gr.Blocks(theme="Nymbo/Alyx_Theme") as app:
63
- # Giriş sayfası
64
  with gr.Group(visible=True) as intro_page:
65
  gr.Markdown("""
66
  ## ❗ Kullanım Şartları ve Sorumluluk Reddi
67
 
68
- **AlpDroid**, ALPERALL tarafından geliştirilen deneysel bir yapay zeka karakteridir.
69
- - Yapay zekadır, tavsiyeleri bağlayıcı değildir
70
- - Bilgi eğlence amaçlıdır
71
- - Sorumluluk kullanıcıya aittir
72
 
73
- Devam etmek için aşağıdaki kutucuğu işaretle.
74
  """)
75
  checkbox = gr.Checkbox(label="✅ Okudum, Onaylıyorum")
76
- btn = gr.Button("🚀 Devam Et")
77
 
78
- # Chat ekranı
79
  with gr.Group(visible=False) as chat_page:
80
  gr.ChatInterface(
81
  fn=generate,
@@ -83,16 +87,19 @@ with gr.Blocks(theme="Nymbo/Alyx_Theme") as app:
83
  show_label=False,
84
  show_share_button=False,
85
  show_copy_button=True,
86
- likeable=True,
87
  layout="panel"
88
  ),
89
  additional_inputs=additional_inputs,
90
- title="📎 AlpDroid Mistral Chat",
91
- description="Ataç ile dosya gönder, konuşmaya dahil et.",
92
- inputs=[gr.Textbox(placeholder="Mesajını yaz..."), gr.File(label="📎 Dosya yükle", file_types=[".txt", ".md", ".csv", ".json"])],
 
 
 
93
  )
94
 
95
- # Geçiş
96
- btn.click(fn=start_chat, inputs=[checkbox], outputs=[intro_page, chat_page])
97
 
 
98
  app.launch()
 
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
+ # Mesaj geçmişini modele uygun formata çevir
8
  def format_prompt(message, history):
9
  prompt = "<s>"
10
  for user_prompt, bot_response in history:
 
12
  prompt += f"[INST] {message} [/INST]"
13
  return prompt
14
 
15
+ # Cevap üret
16
  def generate(message, history, file=None, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.2):
17
+ # Dosya varsa, içeriği mesajın sonuna ekle
 
 
 
18
  if file is not None:
19
  try:
20
  file_content = file.read().decode("utf-8")
21
+ message += f"\n\n📎 Ekli dosyanın içeriği:\n{file_content}"
22
  except Exception as e:
23
+ message += f"\n\n(Dosya okunamadı: {str(e)})"
24
+
25
+ # Model ayarları
26
+ temperature = float(max(temperature, 1e-2))
27
+ top_p = float(top_p)
28
 
29
  formatted_prompt = format_prompt(message, history)
30
 
31
+ generate_kwargs = dict(
 
32
  temperature=temperature,
33
  max_new_tokens=max_new_tokens,
34
  top_p=top_p,
 
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
  return output
49
 
50
+ # Slider ayarları
51
  additional_inputs = [
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("Devam etmek için şartları kabul etmelisin!")
64
 
65
+ # Gradio Arayüzü
66
  with gr.Blocks(theme="Nymbo/Alyx_Theme") as app:
67
+ # Giriş ekranı (şartlar)
68
  with gr.Group(visible=True) as intro_page:
69
  gr.Markdown("""
70
  ## ❗ Kullanım Şartları ve Sorumluluk Reddi
71
 
72
+ **AlpDroid**, ALPERALL tarafından geliştirilen deneysel bir yapay zekadır.
73
+ - Bu bot **gerçek bir kişi değildir**.
74
+ - **Hukuki, tıbbi, finansal ya da etik** kararlar için kullanılmamalıdır.
75
+ - Tüm kullanım **kendi sorumluluğunuzdadır**.
76
 
77
+ Devam etmek için aşağıdaki kutucuğu işaretleyin.
78
  """)
79
  checkbox = gr.Checkbox(label="✅ Okudum, Onaylıyorum")
80
+ continue_btn = gr.Button("🚀 Başla")
81
 
82
+ # Sohbet ekranı
83
  with gr.Group(visible=False) as chat_page:
84
  gr.ChatInterface(
85
  fn=generate,
 
87
  show_label=False,
88
  show_share_button=False,
89
  show_copy_button=True,
 
90
  layout="panel"
91
  ),
92
  additional_inputs=additional_inputs,
93
+ title="🤖 AlpDroid | Mistral 7B Instruct",
94
+ description="📎 Dosya yükleyerek AlpDroid'e veri okutabilir, ayarlarla oynayarak sonuçları değiştirebilirsin.",
95
+ inputs=[
96
+ gr.Textbox(placeholder="Mesajını yaz..."),
97
+ gr.File(label="📎 Dosya yükle", file_types=[".txt", ".md", ".csv", ".json"])
98
+ ],
99
  )
100
 
101
+ # Giriş ekranından chate geçiş
102
+ continue_btn.click(fn=start_chat, inputs=[checkbox], outputs=[intro_page, chat_page])
103
 
104
+ # Uygulamayı başlat
105
  app.launch()