Update app.py
Browse files
app.py
CHANGED
@@ -2,90 +2,57 @@ import gradio as gr
|
|
2 |
from gradio_client import Client, handle_file
|
3 |
from huggingface_hub import InferenceClient
|
4 |
|
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),
|
21 |
prompt="Describe this image.",
|
22 |
api_name="/answer_question"
|
23 |
)
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
# LLaMA API'sine açıklamayı ve kullanıcının mesajını gönderiyoruz
|
29 |
-
history.append({"role": "user", "content": user_message})
|
30 |
-
history.append({"role": "assistant", "content": description})
|
31 |
|
32 |
-
|
33 |
llama_result = llama_client.chat_completion(
|
34 |
-
messages=
|
35 |
-
max_tokens=512,
|
36 |
-
temperature=0.7,
|
37 |
-
top_p=0.95
|
38 |
)
|
39 |
-
|
40 |
-
# LLaMA'nın cevabını history'e ekliyoruz
|
41 |
-
llama_response = llama_result['choices'][0]['message']['content']
|
42 |
-
history.append({"role": "assistant", "content": llama_response})
|
43 |
|
44 |
-
|
45 |
-
return description + "\n\nAssistant: " + llama_response
|
46 |
|
47 |
-
# Sohbet fonksiyonu, resim yüklenip yüklenmediğine göre yönlendirecek
|
48 |
def chat_or_image(image, user_message):
|
49 |
global history
|
50 |
-
|
51 |
-
# Resim yüklenmişse, önce açıklama alıp sonra LLaMA'ya gönderiyoruz
|
52 |
if image:
|
53 |
return describe_image(image, user_message)
|
54 |
else:
|
55 |
-
|
56 |
-
|
57 |
llama_result = llama_client.chat_completion(
|
58 |
-
messages=
|
59 |
max_tokens=512,
|
60 |
temperature=0.7,
|
61 |
top_p=0.95
|
62 |
)
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
image_input = gr.Image(label="Resim Yükleyin", type="filepath", visible=False)
|
75 |
-
|
76 |
-
# Resim ekleme butonunun işlevselliği
|
77 |
-
gr.Button("Resim Ekle", scale=2).click(lambda: image_input.update(visible=True)) # Butona tıklandığında resmi göster
|
78 |
-
|
79 |
-
# Mesaj yazma alanı
|
80 |
-
user_message = gr.Textbox(label="Mesajınızı Yazın", placeholder="Yazınızı buraya yazın...")
|
81 |
-
|
82 |
-
# Sohbetin çıkışı
|
83 |
-
chatbot_output = gr.Chatbot(label="Sohbet", type="messages")
|
84 |
-
|
85 |
-
# Resim ve mesaj gönderme
|
86 |
-
submit_button = gr.Button("Gönder")
|
87 |
-
submit_button.click(chat_or_image, inputs=[image_input, user_message], outputs=chatbot_output)
|
88 |
|
89 |
-
# Gradio uygulamasını başlat
|
90 |
if __name__ == "__main__":
|
91 |
demo.launch(show_error=True)
|
|
|
2 |
from gradio_client import Client, handle_file
|
3 |
from huggingface_hub import InferenceClient
|
4 |
|
|
|
5 |
moondream_client = Client("vikhyatk/moondream2")
|
|
|
|
|
6 |
llama_client = InferenceClient("Qwen/QwQ-32B-Preview")
|
7 |
|
|
|
8 |
history = []
|
9 |
|
|
|
10 |
def describe_image(image, user_message):
|
11 |
global history
|
|
|
|
|
12 |
result = moondream_client.predict(
|
13 |
img=handle_file(image),
|
14 |
prompt="Describe this image.",
|
15 |
api_name="/answer_question"
|
16 |
)
|
17 |
|
18 |
+
description = result
|
19 |
+
history.append(f"User: {user_message}")
|
20 |
+
history.append(f"Assistant: {description}")
|
|
|
|
|
|
|
21 |
|
22 |
+
full_conversation = "\n".join(history)
|
23 |
llama_result = llama_client.chat_completion(
|
24 |
+
messages=[{"role": "user", "content": full_conversation}],
|
25 |
+
max_tokens=512,
|
26 |
+
temperature=0.7,
|
27 |
+
top_p=0.95
|
28 |
)
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
return description + "\n\nAssistant: " + llama_result['choices'][0]['message']['content']
|
|
|
31 |
|
|
|
32 |
def chat_or_image(image, user_message):
|
33 |
global history
|
|
|
|
|
34 |
if image:
|
35 |
return describe_image(image, user_message)
|
36 |
else:
|
37 |
+
history.append(f"User: {user_message}")
|
38 |
+
full_conversation = "\n".join(history)
|
39 |
llama_result = llama_client.chat_completion(
|
40 |
+
messages=[{"role": "user", "content": full_conversation}],
|
41 |
max_tokens=512,
|
42 |
temperature=0.7,
|
43 |
top_p=0.95
|
44 |
)
|
45 |
+
return llama_result['choices'][0]['message']['content']
|
46 |
+
|
47 |
+
demo = gr.Interface(
|
48 |
+
fn=chat_or_image,
|
49 |
+
inputs=[
|
50 |
+
gr.Image(type="filepath", label="Upload Image (optional)", elem_id="left-column"),
|
51 |
+
gr.Textbox(label="Ask or Chat", placeholder="Ask a question...", lines=2, elem_id="right-column")
|
52 |
+
],
|
53 |
+
outputs="text",
|
54 |
+
layout="horizontal", # Adjust the layout to horizontal
|
55 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
|
|
57 |
if __name__ == "__main__":
|
58 |
demo.launch(show_error=True)
|