Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,54 +2,42 @@ 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 |
-
# 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,
|
| 37 |
-
temperature=0.7,
|
| 38 |
-
top_p=0.95
|
| 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(
|
|
@@ -60,15 +48,14 @@ def chat_or_image(image, user_message):
|
|
| 60 |
)
|
| 61 |
return llama_result['choices'][0]['message']['content']
|
| 62 |
|
| 63 |
-
# Gradio arayüzü
|
| 64 |
demo = gr.Interface(
|
| 65 |
-
fn=chat_or_image,
|
| 66 |
inputs=[
|
| 67 |
-
gr.Image(type="filepath", label="Resim Yükle (isteğe bağlı)"),
|
| 68 |
-
gr.Textbox(label="Soru Sor ya da Konuş", placeholder="Soru sor...", lines=2)
|
| 69 |
],
|
| 70 |
-
outputs="text",
|
| 71 |
)
|
| 72 |
|
| 73 |
if _name_ == "_main_":
|
| 74 |
-
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 |
|
|
|
|
| 7 |
llama_client = InferenceClient("Qwen/QwQ-32B-Preview")
|
| 8 |
|
|
|
|
| 9 |
history = []
|
| 10 |
|
|
|
|
| 11 |
def describe_image(image, user_message):
|
| 12 |
global history
|
| 13 |
|
|
|
|
| 14 |
result = moondream_client.predict(
|
| 15 |
img=handle_file(image),
|
| 16 |
prompt="Describe this image.",
|
| 17 |
api_name="/answer_question"
|
| 18 |
)
|
| 19 |
|
|
|
|
| 20 |
description = result # Moondream2'nin cevabını alıyoruz
|
| 21 |
|
|
|
|
| 22 |
history.append(f"User: {user_message}")
|
| 23 |
history.append(f"Assistant: {description}")
|
| 24 |
|
|
|
|
| 25 |
full_conversation = "\n".join(history)
|
| 26 |
llama_result = llama_client.chat_completion(
|
| 27 |
messages=[{"role": "user", "content": full_conversation}],
|
| 28 |
+
max_tokens=512,
|
| 29 |
+
temperature=0.7,
|
| 30 |
+
top_p=0.95
|
| 31 |
)
|
| 32 |
|
|
|
|
| 33 |
return description + "\n\nAssistant: " + llama_result['choices'][0]['message']['content']
|
| 34 |
|
|
|
|
| 35 |
def chat_or_image(image, user_message):
|
| 36 |
global history
|
| 37 |
|
|
|
|
| 38 |
if image:
|
| 39 |
return describe_image(image, user_message)
|
| 40 |
else:
|
|
|
|
| 41 |
history.append(f"User: {user_message}")
|
| 42 |
full_conversation = "\n".join(history)
|
| 43 |
llama_result = llama_client.chat_completion(
|
|
|
|
| 48 |
)
|
| 49 |
return llama_result['choices'][0]['message']['content']
|
| 50 |
|
|
|
|
| 51 |
demo = gr.Interface(
|
| 52 |
+
fn=chat_or_image,
|
| 53 |
inputs=[
|
| 54 |
+
gr.Image(type="filepath", label="Resim Yükle (isteğe bağlı)"),
|
| 55 |
+
gr.Textbox(label="Soru Sor ya da Konuş", placeholder="Soru sor...", lines=2)
|
| 56 |
],
|
| 57 |
+
outputs="text",
|
| 58 |
)
|
| 59 |
|
| 60 |
if _name_ == "_main_":
|
| 61 |
+
demo.launch(show_error=True)
|