Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,23 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
from gradio_client import Client, handle_file
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
client =
|
| 7 |
|
| 8 |
-
#
|
| 9 |
def describe_image(image):
|
| 10 |
-
# Call the external API to get a description of the image
|
| 11 |
result = client.predict(
|
| 12 |
-
img=handle_file(image),
|
| 13 |
-
prompt="Describe this image.",
|
| 14 |
-
api_name="/answer_question"
|
| 15 |
)
|
| 16 |
return result
|
| 17 |
|
| 18 |
-
|
| 19 |
-
message,
|
| 20 |
-
history: list[tuple[str, str]],
|
| 21 |
-
system_message,
|
| 22 |
-
max_tokens,
|
| 23 |
-
temperature,
|
| 24 |
-
top_p,
|
| 25 |
-
image
|
| 26 |
-
):
|
| 27 |
-
messages = [{"role": "system", "content": system_message}]
|
| 28 |
-
|
| 29 |
-
for val in history:
|
| 30 |
-
if val[0]:
|
| 31 |
-
messages.append({"role": "user", "content": val[0]})
|
| 32 |
-
if val[1]:
|
| 33 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 34 |
-
|
| 35 |
-
# Process the image if provided
|
| 36 |
-
if image:
|
| 37 |
-
description = describe_image(image)
|
| 38 |
-
return description
|
| 39 |
-
|
| 40 |
-
# If no image, proceed with the usual chat flow
|
| 41 |
-
messages.append({"role": "user", "content": message})
|
| 42 |
-
|
| 43 |
-
response = ""
|
| 44 |
-
|
| 45 |
-
for message in client.chat_completion(
|
| 46 |
-
messages,
|
| 47 |
-
max_tokens=max_tokens,
|
| 48 |
-
stream=True,
|
| 49 |
-
temperature=temperature,
|
| 50 |
-
top_p=top_p,
|
| 51 |
-
):
|
| 52 |
-
token = message.choices[0].delta.content
|
| 53 |
-
|
| 54 |
-
response += token
|
| 55 |
-
yield response
|
| 56 |
-
|
| 57 |
-
# Set up the Gradio interface with image input and output for description
|
| 58 |
demo = gr.Interface(
|
| 59 |
-
fn=
|
| 60 |
-
inputs=
|
| 61 |
-
|
| 62 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 63 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 64 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 65 |
-
gr.Image(type="file", label="Upload Image") # Allow image upload
|
| 66 |
-
],
|
| 67 |
-
outputs="text", # Display the description as text output
|
| 68 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
|
|
|
| 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__":
|