suayptalha commited on
Commit
fab8ffe
·
verified ·
1 Parent(s): 8a5872a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -14
app.py CHANGED
@@ -1,23 +1,65 @@
1
  import gradio as gr
2
  from gradio_client import Client, handle_file
3
 
4
- # Moondream2 API'sine bağlanmak için Client'ı başlatıyoruz
5
- client = Client("vikhyatk/moondream2")
6
-
7
- # Resmi açıklamak için kullanılan fonksiyon
8
- def describe_image(image):
9
- result = client.predict(
10
- img=handle_file(image), # Kullanıcının yüklediği resmi API'ye gönderiyoruz
11
- prompt="Describe this image.", # Resmi açıklama isteği
12
- api_name="/answer_question" # API'nin doğru endpoint'i
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  )
14
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- # Gradio arayüzü tanımlıyoruz
17
  demo = gr.Interface(
18
- fn=describe_image, # Resmi açıklamak için fonksiyon
19
- inputs=gr.Image(type="filepath", label="Resim Yükle"), # Resim yükleme alanı
20
- outputs="text", # Çıktı olarak metin (resmin açıklaması) alacağız
 
 
 
21
  )
22
 
23
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from gradio_client import Client, handle_file
3
 
4
+ # Moondream2 ve LLaMA API'lerine bağlanmak için Client'ları başlatıyoruz
5
+ moondream_client = Client("vikhyatk/moondream2")
6
+ llama_client = Client("goingyt/meta-llama-Llama-3.3-70B-Instruct")
7
+
8
+ # Sohbet geçmişini tutmak için bir değişken
9
+ history = []
10
+
11
+ # Resim açıklama fonksiyonu
12
+ def describe_image(image, user_message):
13
+ global history
14
+
15
+ # Resmi Moondream2 API'sine gönderiyoruz
16
+ result = moondream_client.predict(
17
+ img=handle_file(image),
18
+ prompt="Describe this image.",
19
+ api_name="/answer_question"
20
+ )
21
+
22
+ # Moondream2'den alınan açıklamayı sisteme dahil ediyoruz
23
+ description = result # Moondream2'nin cevabını alıyoruz
24
+
25
+ # LLaMA API'sine açıklamayı ve kullanıcının mesajını gönderiyoruz
26
+ history.append(("User", user_message))
27
+ history.append(("Assistant", description))
28
+
29
+ llama_result = llama_client.predict(
30
+ message=user_message,
31
+ history=history,
32
+ api_name="/chat"
33
  )
34
+
35
+ # Sonucu döndürüyoruz
36
+ return description + "\n\nAssistant: " + llama_result
37
+
38
+ # Sohbet fonksiyonu, resim yüklenip yüklenmediğine göre yönlendirecek
39
+ def chat_or_image(image, user_message):
40
+ global history
41
+
42
+ # Resim yüklenmişse, önce açıklama alıp sonra LLaMA'ya gönderiyoruz
43
+ if image:
44
+ return describe_image(image, user_message)
45
+ else:
46
+ # Resim yoksa, direkt LLaMA'ya mesajı gönderiyoruz
47
+ history.append(("User", user_message))
48
+ llama_result = llama_client.predict(
49
+ message=user_message,
50
+ history=history,
51
+ api_name="/chat"
52
+ )
53
+ return llama_result
54
 
55
+ # Gradio arayüzü
56
  demo = gr.Interface(
57
+ fn=chat_or_image, # Hem resim hem de metin için kullanılacak fonksiyon
58
+ inputs=[
59
+ gr.Image(type="filepath", label="Resim Yükle (isteğe bağlı)"), # Resim yükleme
60
+ gr.Textbox(label="Soru Sor ya da Konuş", placeholder="Soru sor...", lines=2) # Metin girişi
61
+ ],
62
+ outputs="text", # Çıktı metin olarak dönecek
63
  )
64
 
65
  if __name__ == "__main__":