Update app.py
Browse files
app.py
CHANGED
@@ -5,17 +5,16 @@ from huggingface_hub import InferenceClient
|
|
5 |
# Moondream2 için Client kullanıyoruz
|
6 |
moondream_client = Client("vikhyatk/moondream2")
|
7 |
|
8 |
-
#
|
9 |
llama_client = InferenceClient("Qwen/QwQ-32B-Preview")
|
10 |
|
11 |
-
# Sohbet
|
12 |
history = []
|
13 |
|
14 |
# Resim açıklama fonksiyonu
|
15 |
-
def describe_image(image, user_message
|
16 |
-
|
17 |
-
|
18 |
-
return "No image provided", history # Hata mesajı döndür
|
19 |
# Resmi Moondream2 API'sine gönderiyoruz
|
20 |
result = moondream_client.predict(
|
21 |
img=handle_file(image),
|
@@ -23,70 +22,53 @@ def describe_image(image, user_message, history):
|
|
23 |
api_name="/answer_question"
|
24 |
)
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
history.append({"role": "assistant", "content": description}) # string olarak
|
29 |
-
|
30 |
-
return description, history
|
31 |
-
|
32 |
-
# Text ve history ile sohbet fonksiyonu
|
33 |
-
def chat_with_text(user_message, history, max_new_tokens=250):
|
34 |
-
# Kullanıcı mesajını history'ye ekliyoruz
|
35 |
-
history.append({"role": "user", "content": user_message}) # string olarak
|
36 |
|
37 |
-
#
|
38 |
-
|
|
|
|
|
|
|
|
|
39 |
llama_result = llama_client.chat_completion(
|
40 |
-
messages=
|
41 |
-
max_tokens=
|
42 |
-
temperature=0.7,
|
43 |
-
top_p=0.95
|
44 |
)
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
history
|
49 |
-
|
50 |
-
return assistant_reply, history
|
51 |
-
|
52 |
-
# Resim ve/veya metin tabanlı sohbet fonksiyonu
|
53 |
-
def bot_streaming(message, history=None, max_new_tokens=250):
|
54 |
-
if history is None: # Eğer `history` verilmemişse boş bir liste kullanıyoruz
|
55 |
-
history = []
|
56 |
-
|
57 |
-
user_message = message.get("text", "")
|
58 |
-
image = message.get("image", None)
|
59 |
-
|
60 |
-
if image: # Resim varsa
|
61 |
-
response, history = describe_image(image, user_message, history)
|
62 |
-
else: # Sadece metin mesajı varsa
|
63 |
-
response, history = chat_with_text(user_message, history, max_new_tokens)
|
64 |
|
65 |
-
#
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
# Gradio arayüzü
|
69 |
-
demo = gr.
|
70 |
-
fn=
|
71 |
-
|
72 |
-
|
73 |
-
gr.
|
74 |
-
minimum=10,
|
75 |
-
maximum=500,
|
76 |
-
value=250,
|
77 |
-
step=10,
|
78 |
-
label="Maximum number of new tokens to generate",
|
79 |
-
)
|
80 |
],
|
81 |
-
|
82 |
-
"This demo combines text and image understanding using Moondream2 for visual "
|
83 |
-
"tasks and Qwen/QwQ-32B-Preview for conversational AI. Upload an image, ask questions, "
|
84 |
-
"or just chat!"
|
85 |
-
),
|
86 |
-
stop_btn="Stop Generation",
|
87 |
-
fill_height=True,
|
88 |
-
multimodal=True,
|
89 |
)
|
90 |
|
91 |
-
if
|
92 |
-
demo.launch(
|
|
|
5 |
# Moondream2 için Client kullanıyoruz
|
6 |
moondream_client = Client("vikhyatk/moondream2")
|
7 |
|
8 |
+
# LLaMA için InferenceClient kullanıyoruz
|
9 |
llama_client = InferenceClient("Qwen/QwQ-32B-Preview")
|
10 |
|
11 |
+
# Sohbet geçmişini tutmak için bir değişken
|
12 |
history = []
|
13 |
|
14 |
# Resim açıklama fonksiyonu
|
15 |
+
def describe_image(image, user_message):
|
16 |
+
global history
|
17 |
+
|
|
|
18 |
# Resmi Moondream2 API'sine gönderiyoruz
|
19 |
result = moondream_client.predict(
|
20 |
img=handle_file(image),
|
|
|
22 |
api_name="/answer_question"
|
23 |
)
|
24 |
|
25 |
+
# Moondream2'den alınan açıklamayı sisteme dahil ediyoruz
|
26 |
+
description = result # Moondream2'nin cevabını alıyoruz
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
# LLaMA API'sine açıklamayı ve kullanıcının mesajını gönderiyoruz
|
29 |
+
history.append(f"User: {user_message}")
|
30 |
+
history.append(f"Assistant: {description}")
|
31 |
+
|
32 |
+
# Sohbet geçmişini birleştirip tek bir mesaj olarak LLaMA'ya gönderiyoruz
|
33 |
+
full_conversation = "\n".join(history)
|
34 |
llama_result = llama_client.chat_completion(
|
35 |
+
messages=[{"role": "user", "content": full_conversation}],
|
36 |
+
max_tokens=512, # Burada token sayısını belirleyebilirsiniz
|
37 |
+
temperature=0.7, # Sıcaklık parametresi
|
38 |
+
top_p=0.95 # Nucleus sampling için top_p parametresi
|
39 |
)
|
40 |
+
|
41 |
+
# Sonucu döndürüyoruz
|
42 |
+
return description + "\n\nAssistant: " + llama_result['choices'][0]['message']['content']
|
43 |
|
44 |
+
# Sohbet fonksiyonu, resim yüklenip yüklenmediğine göre yönlendirecek
|
45 |
+
def chat_or_image(image, user_message):
|
46 |
+
global history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
# Resim yüklenmişse, önce açıklama alıp sonra LLaMA'ya gönderiyoruz
|
49 |
+
if image:
|
50 |
+
return describe_image(image, user_message)
|
51 |
+
else:
|
52 |
+
# Resim yoksa, direkt LLaMA'ya mesajı gönderiyoruz
|
53 |
+
history.append(f"User: {user_message}")
|
54 |
+
full_conversation = "\n".join(history)
|
55 |
+
llama_result = llama_client.chat_completion(
|
56 |
+
messages=[{"role": "user", "content": full_conversation}],
|
57 |
+
max_tokens=512,
|
58 |
+
temperature=0.7,
|
59 |
+
top_p=0.95
|
60 |
+
)
|
61 |
+
return llama_result['choices'][0]['message']['content']
|
62 |
|
63 |
# Gradio arayüzü
|
64 |
+
demo = gr.Interface(
|
65 |
+
fn=chat_or_image, # Hem resim hem de metin için kullanılacak fonksiyon
|
66 |
+
inputs=[
|
67 |
+
gr.Image(type="filepath", label="Resim Yükle (isteğe bağlı)"), # Resim yükleme
|
68 |
+
gr.Textbox(label="Soru Sor ya da Konuş", placeholder="Soru sor...", lines=2) # Metin girişi
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
],
|
70 |
+
outputs="text", # Çıktı metin olarak dönecek
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
)
|
72 |
|
73 |
+
if _name_ == "_main_":
|
74 |
+
demo.launch(show_error=True) # Hata raporlamayı etkinleştiriyoruz
|