Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,73 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
|
|
|
|
|
4 |
|
5 |
-
def
|
6 |
-
|
7 |
-
|
8 |
-
return greeting, lucky_number
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
theme="gradio/seafoam" # <-- String representation of Theme passed to theme= parameter
|
27 |
-
)
|
28 |
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
|
4 |
+
# InferenceClient'ı uygun model ile başlatıyoruz
|
5 |
+
client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
|
6 |
|
7 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
8 |
+
# Mesajları hazırlıyoruz, sistem mesajı ile başlıyoruz
|
9 |
+
messages = [{"role": "system", "content": system_message}]
|
|
|
10 |
|
11 |
+
# Geçmişteki kullanıcı ve asistan mesajlarını ekliyoruz
|
12 |
+
for user_msg, assistant_msg in history:
|
13 |
+
if user_msg:
|
14 |
+
messages.append({"role": "user", "content": user_msg})
|
15 |
+
if assistant_msg:
|
16 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
17 |
|
18 |
+
# Son kullanıcı mesajını ekliyoruz
|
19 |
+
messages.append({"role": "user", "content": message})
|
20 |
|
21 |
+
response = ""
|
22 |
+
try:
|
23 |
+
# Yanıtları alıyoruz ve yayınlıyoruz
|
24 |
+
for message in client.chat_completion(
|
25 |
+
messages=messages,
|
26 |
+
max_tokens=max_tokens,
|
27 |
+
stream=True,
|
28 |
+
temperature=temperature,
|
29 |
+
top_p=top_p,
|
30 |
+
):
|
31 |
+
token = message.choices[0].delta.get('content', '')
|
32 |
+
response += token
|
33 |
+
yield response
|
34 |
+
except Exception as e:
|
35 |
+
# Hata durumunda hata mesajını döndürüyoruz
|
36 |
+
yield f"Hata: {e}"
|
37 |
|
38 |
+
# Gradio arayüzünü oluşturuyoruz
|
39 |
+
with gr.Blocks(theme=gr.themes.Base()) as demo: # Dark theme can be specified here
|
40 |
+
system_message = gr.Textbox(value="You are a GPT-2 model chatbot named Simulacra. You are produced by Anezatra.", label="Sistem mesajı")
|
41 |
+
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Maksimum yeni token sayısı")
|
42 |
+
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Sıcaklık (Temperature)")
|
43 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nükleus örnekleme)")
|
|
|
|
|
44 |
|
45 |
+
chatbot = gr.Chatbot()
|
46 |
+
msg = gr.Textbox(label="Mesajınızı yazın")
|
47 |
+
clear = gr.Button("Temizle")
|
48 |
+
|
49 |
+
def user_input(user_message, history):
|
50 |
+
return "", history + [[user_message, None]]
|
51 |
+
|
52 |
+
def bot_response(history):
|
53 |
+
last_message = history[-1][0]
|
54 |
+
response_gen = respond(
|
55 |
+
message=last_message,
|
56 |
+
history=history[:-1],
|
57 |
+
system_message=system_message.value,
|
58 |
+
max_tokens=max_tokens.value,
|
59 |
+
temperature=temperature.value,
|
60 |
+
top_p=top_p.value,
|
61 |
+
)
|
62 |
+
for response in response_gen:
|
63 |
+
history[-1][1] = response
|
64 |
+
yield history
|
65 |
+
|
66 |
+
msg.submit(user_input, [msg, chatbot], [msg, chatbot], queue=False).then(
|
67 |
+
bot_response, chatbot, chatbot
|
68 |
+
)
|
69 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
70 |
+
|
71 |
+
# Uygulamayı başlatıyoruz
|
72 |
+
if __name__ == "__main__":
|
73 |
+
demo.launch()
|