suayptalha's picture
Update app.py
4dff0d4 verified
raw
history blame
2.67 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şi
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"
)
description = result # Moondream2'den açıklama alıyoruz
history.append({"role": "user", "content": user_message})
history.append({"role": "assistant", "content": description})
return description
# Text ve history ile sohbet fonksiyonu
def chat_with_text(user_message, max_new_tokens=250):
global history
# Kullanıcı mesajını history'ye ekliyoruz
history.append({"role": "user", "content": user_message})
# Tüm geçmişi LLaMA'ya gönderiyoruz
texts = [{"role": msg["role"], "content": msg["content"]} for msg in history]
llama_result = llama_client.chat_completion(
messages=texts,
max_tokens=max_new_tokens,
temperature=0.7,
top_p=0.95
)
# Asistan cevabını alıyoruz ve history'ye ekliyoruz
assistant_reply = llama_result["choices"][0]["message"]["content"]
history.append({"role": "assistant", "content": assistant_reply})
return assistant_reply
# Resim ve/veya metin tabanlı sohbet fonksiyonu
def bot_streaming(message, max_new_tokens=250):
global history
user_message = message.get("text", "")
image = message.get("image", None)
if image: # Resim varsa
response = describe_image(image, user_message)
else: # Sadece metin mesajı varsa
response = chat_with_text(user_message, max_new_tokens)
return response
# Gradio arayüzü
demo = gr.ChatInterface(
fn=bot_streaming,
title="Multimodal Chat Assistant",
additional_inputs=[
gr.Slider(
minimum=10,
maximum=500,
value=250,
step=10,
label="Maximum number of new tokens to generate",
)
],
description=(
"This demo combines text and image understanding using Moondream2 for visual "
"tasks and LLaMA for conversational AI. Upload an image, ask questions, "
"or just chat!"
),
stop_btn="Stop Generation",
fill_height=True,
multimodal=True,
)
if __name__ == "__main__":
demo.launch(debug=True)