Update app.py
Browse files
app.py
CHANGED
@@ -2,91 +2,72 @@ import gradio as gr
|
|
2 |
from gradio_client import Client, handle_file
|
3 |
from huggingface_hub import InferenceClient
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
# LLaMA için InferenceClient kullanıyoruz
|
9 |
-
llama_client = InferenceClient("Qwen/QwQ-32B-Preview")
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
prompt="Describe this image.",
|
20 |
api_name="/answer_question"
|
21 |
)
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
history.append({"role": "assistant", "content": description}) # string olarak
|
26 |
-
|
27 |
-
# Resim açıklamasına alt metin (alt_text) ekliyoruz
|
28 |
-
image_message = {
|
29 |
-
"type": "image",
|
30 |
-
"data": image,
|
31 |
-
"alt_text": description # Resmin açıklaması olarak alt metni ekliyoruz
|
32 |
-
}
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
# Tüm geçmişi LLaMA'ya gönderiyoruz
|
42 |
-
texts = [{"role": msg["role"], "content": msg["content"]} for msg in history]
|
43 |
-
llama_result = llama_client.chat_completion(
|
44 |
-
messages=texts,
|
45 |
-
max_tokens=max_new_tokens,
|
46 |
-
temperature=0.7,
|
47 |
-
top_p=0.95
|
48 |
-
)
|
49 |
-
|
50 |
-
# Asistan cevabını alıyoruz ve history'ye ekliyoruz
|
51 |
-
assistant_reply = llama_result["choices"][0]["message"]["content"]
|
52 |
-
history.append({"role": "assistant", "content": assistant_reply}) # string olarak
|
53 |
-
|
54 |
-
return assistant_reply, history
|
55 |
|
56 |
-
#
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
70 |
|
71 |
-
# Gradio
|
72 |
demo = gr.ChatInterface(
|
73 |
-
|
74 |
-
title="Multimodal Chat Assistant",
|
75 |
additional_inputs=[
|
76 |
-
gr.Textbox(value="You are a friendly assistant.", label="System message"),
|
77 |
-
gr.Slider(minimum=
|
78 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
79 |
-
gr.Slider(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
],
|
81 |
-
description=(
|
82 |
-
"This demo combines text and image understanding using Moondream2 for visual "
|
83 |
-
"tasks and LLaMA 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 __name__ == "__main__":
|
92 |
-
demo.launch(
|
|
|
2 |
from gradio_client import Client, handle_file
|
3 |
from huggingface_hub import InferenceClient
|
4 |
|
5 |
+
# Initialize the InferenceClient for FastLlama model
|
6 |
+
client = InferenceClient("Qwen/QwQ-32B-Preview")
|
|
|
|
|
|
|
7 |
|
8 |
+
# Initialize the Moondream Client for image description
|
9 |
+
moondream_client = Client("vikhyatk/moondream2")
|
10 |
|
11 |
+
def respond(
|
12 |
+
message,
|
13 |
+
history: list[tuple[str, str]],
|
14 |
+
system_message,
|
15 |
+
max_tokens,
|
16 |
+
temperature,
|
17 |
+
top_p,
|
18 |
+
image_input
|
19 |
+
):
|
20 |
+
# Step 1: Handle the image and get its description using Moondream API
|
21 |
+
image_file = handle_file(image_input)
|
22 |
+
image_description = moondream_client.predict(
|
23 |
+
img=image_file,
|
24 |
prompt="Describe this image.",
|
25 |
api_name="/answer_question"
|
26 |
)
|
27 |
|
28 |
+
# Step 2: Create the messages for the chat model
|
29 |
+
messages = [{"role": "system", "content": system_message}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
# Add history to the messages
|
32 |
+
for val in history:
|
33 |
+
if val[0]:
|
34 |
+
messages.append({"role": "user", "content": val[0]})
|
35 |
+
if val[1]:
|
36 |
+
messages.append({"role": "assistant", "content": val[1]})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
# Add the image description to the user message
|
39 |
+
messages.append({"role": "user", "content": f"Here is the description of the image: {image_description}. Can you comment on it?"})
|
40 |
+
|
41 |
+
# Step 3: Get the response from the assistant
|
42 |
+
response = ""
|
43 |
+
for message in client.chat_completion(
|
44 |
+
messages,
|
45 |
+
max_tokens=max_tokens,
|
46 |
+
stream=True,
|
47 |
+
temperature=temperature,
|
48 |
+
top_p=top_p,
|
49 |
+
):
|
50 |
+
token = message.choices[0].delta.content
|
51 |
+
response += token
|
52 |
+
yield response
|
53 |
|
54 |
+
# Set up Gradio interface
|
55 |
demo = gr.ChatInterface(
|
56 |
+
respond,
|
|
|
57 |
additional_inputs=[
|
58 |
+
gr.Textbox(value="You are a friendly assistant named FastLlama.", label="System message"),
|
59 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
60 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
61 |
+
gr.Slider(
|
62 |
+
minimum=0.1,
|
63 |
+
maximum=1.0,
|
64 |
+
value=0.95,
|
65 |
+
step=0.05,
|
66 |
+
label="Top-p (nucleus sampling)",
|
67 |
+
),
|
68 |
+
gr.Image(type="pil", label="Upload Image for Description") # Image input
|
69 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
)
|
71 |
|
72 |
if __name__ == "__main__":
|
73 |
+
demo.launch()
|