suayptalha's picture
Update app.py
e719a30 verified
raw
history blame
2.73 kB
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