Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,18 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
8 |
|
|
|
|
|
9 |
|
|
|
10 |
def respond(
|
11 |
message,
|
12 |
history: list[tuple[str, str]],
|
@@ -16,33 +22,39 @@ def respond(
|
|
16 |
top_p,
|
17 |
):
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
|
|
20 |
for val in history:
|
21 |
if val[0]:
|
22 |
messages.append({"role": "user", "content": val[0]})
|
23 |
if val[1]:
|
24 |
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
|
|
|
|
27 |
|
|
|
28 |
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
messages,
|
32 |
max_tokens=max_tokens,
|
33 |
stream=True,
|
34 |
temperature=temperature,
|
35 |
top_p=top_p,
|
36 |
):
|
37 |
-
token =
|
38 |
-
|
39 |
response += token
|
40 |
-
yield response
|
41 |
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
demo = gr.ChatInterface(
|
47 |
respond,
|
48 |
additional_inputs=[
|
@@ -59,6 +71,6 @@ demo = gr.ChatInterface(
|
|
59 |
],
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
1 |
+
import asyncio
|
2 |
+
from googletrans import Translator
|
3 |
import gradio as gr
|
4 |
from huggingface_hub import InferenceClient
|
5 |
|
6 |
+
# Google Translate fonksiyonu (asenkron şekilde)
|
7 |
+
async def translate_text(text, src_lang='tr', dest_lang='en'):
|
8 |
+
translator = Translator()
|
9 |
+
translated = await asyncio.to_thread(translator.translate, text, src=src_lang, dest=dest_lang)
|
10 |
+
return translated.text
|
11 |
|
12 |
+
# Hugging Face modelini kullanmak için client
|
13 |
+
client = InferenceClient("suayptalha/arrLlama")
|
14 |
|
15 |
+
# Modelin yanıtını döndürme fonksiyonu
|
16 |
def respond(
|
17 |
message,
|
18 |
history: list[tuple[str, str]],
|
|
|
22 |
top_p,
|
23 |
):
|
24 |
messages = [{"role": "system", "content": system_message}]
|
25 |
+
|
26 |
+
# History'yi mesajlara ekleyelim
|
27 |
for val in history:
|
28 |
if val[0]:
|
29 |
messages.append({"role": "user", "content": val[0]})
|
30 |
if val[1]:
|
31 |
messages.append({"role": "assistant", "content": val[1]})
|
32 |
|
33 |
+
# Kullanıcıdan gelen mesajı İngilizce'ye çevir
|
34 |
+
translated_message_en = asyncio.run(translate_text(message, src_lang='tr', dest_lang='en'))
|
35 |
+
messages.append({"role": "user", "content": translated_message_en})
|
36 |
|
37 |
+
# Modelden gelen yanıt
|
38 |
response = ""
|
39 |
+
for msg in client.chat_completion(
|
|
|
40 |
messages,
|
41 |
max_tokens=max_tokens,
|
42 |
stream=True,
|
43 |
temperature=temperature,
|
44 |
top_p=top_p,
|
45 |
):
|
46 |
+
token = msg.choices[0].delta.content
|
|
|
47 |
response += token
|
|
|
48 |
|
49 |
+
# Modelin cevabını Türkçe'ye çevir (sadece kullanıcıya gösterilecek)
|
50 |
+
translated_response_tr = asyncio.run(translate_text(response, src_lang='en', dest_lang='tr'))
|
51 |
+
|
52 |
+
# History'ye sadece İngilizce mesaj ekle
|
53 |
+
history.append((translated_message_en, response)) # İngilizce'yi ekliyoruz
|
54 |
|
55 |
+
yield translated_response_tr # Kullanıcıya Türkçe cevabı gösteriyoruz
|
56 |
+
|
57 |
+
# Gradio arayüzü
|
58 |
demo = gr.ChatInterface(
|
59 |
respond,
|
60 |
additional_inputs=[
|
|
|
71 |
],
|
72 |
)
|
73 |
|
74 |
+
# Arayüz başlatma
|
75 |
if __name__ == "__main__":
|
76 |
demo.launch()
|