File size: 2,725 Bytes
87ce80f d8ff437 77d3dbe 87ce80f e719a30 fab8ffe e719a30 9bd7332 fab8ffe e719a30 fab8ffe e719a30 fab8ffe e719a30 fab8ffe e719a30 1c449ee fab8ffe e719a30 1c449ee e719a30 fab8ffe e719a30 fab8ffe e719a30 fab8ffe e719a30 fab8ffe e719a30 1c449ee e719a30 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import gradio as gr
from gradio_client import Client, handle_file
from huggingface_hub import InferenceClient
# Moondream2 için Client kullanıyoruz
moondream_client = Client("vikhyatk/moondream2")
# LLaMA için InferenceClient kullanıyoruz
llama_client = InferenceClient("Qwen/QwQ-32B-Preview")
# Sohbet geçmişini tutmak için bir değişken
history = []
# Resim açıklama fonksiyonu
def describe_image(image, user_message):
global history
# Resmi Moondream2 API'sine gönderiyoruz
result = moondream_client.predict(
img=handle_file(image),
prompt="Describe this image.",
api_name="/answer_question"
)
# Moondream2'den alınan açıklamayı sisteme dahil ediyoruz
description = result # Moondream2'nin cevabını alıyoruz
# LLaMA API'sine açıklamayı ve kullanıcının mesajını gönderiyoruz
history.append(f"User: {user_message}")
history.append(f"Assistant: {description}")
# Sohbet geçmişini birleştirip tek bir mesaj olarak LLaMA'ya gönderiyoruz
full_conversation = "\n".join(history)
llama_result = llama_client.chat_completion(
messages=[{"role": "user", "content": full_conversation}],
max_tokens=512, # Burada token sayısını belirleyebilirsiniz
temperature=0.7, # Sıcaklık parametresi
top_p=0.95 # Nucleus sampling için top_p parametresi
)
# Sonucu döndürüyoruz
return description + "\n\nAssistant: " + llama_result['choices'][0]['message']['content']
# Sohbet fonksiyonu, resim yüklenip yüklenmediğine göre yönlendirecek
def chat_or_image(image, user_message):
global history
# Resim yüklenmişse, önce açıklama alıp sonra LLaMA'ya gönderiyoruz
if image:
return describe_image(image, user_message)
else:
# Resim yoksa, direkt LLaMA'ya mesajı gönderiyoruz
history.append(f"User: {user_message}")
full_conversation = "\n".join(history)
llama_result = llama_client.chat_completion(
messages=[{"role": "user", "content": full_conversation}],
max_tokens=512,
temperature=0.7,
top_p=0.95
)
return llama_result['choices'][0]['message']['content']
# Gradio arayüzü
demo = gr.Interface(
fn=chat_or_image, # Hem resim hem de metin için kullanılacak fonksiyon
inputs=[
gr.Image(type="filepath", label="Upload image (Optional)"), # Resim yükleme
gr.Textbox(label="Ask anything", placeholder="Soru sor...", lines=2) # Metin girişi
],
outputs="text", # Çıktı metin olarak dönecek
)
if _name_ == "_main_":
demo.launch(show_error=True) # Hata raporlamayı etkinleştiriyoruz |