suayptalha commited on
Commit
32b0b26
·
verified ·
1 Parent(s): b718eb0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -15
app.py CHANGED
@@ -1,12 +1,18 @@
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("suayptalha/arrLlama")
 
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
- messages.append({"role": "user", "content": message})
 
 
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 = message.choices[0].delta.content
38
-
39
  response += token
40
- yield response
41
 
 
 
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
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()