Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
from transformers import (
|
| 4 |
+
AutoProcessor, LlavaForConditionalGeneration,
|
| 5 |
+
IdeficsForConditionalGeneration,
|
| 6 |
+
FuyuForCausalLM,
|
| 7 |
+
)
|
| 8 |
+
import torch
|
| 9 |
+
from PIL import Image
|
| 10 |
+
|
| 11 |
+
# === 1. Chat Model ===
|
| 12 |
+
chat_client = InferenceClient("openchat/openchat-3.5-1210")
|
| 13 |
+
|
| 14 |
+
# === 2. LLaVA 1.6 (Mistral) ===
|
| 15 |
+
llava_proc = AutoProcessor.from_pretrained("llava-hf/llava-1.6-mistral-7b-hf")
|
| 16 |
+
llava_model = LlavaForConditionalGeneration.from_pretrained(
|
| 17 |
+
"llava-hf/llava-1.6-mistral-7b-hf", torch_dtype=torch.float16, device_map="auto"
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# === 3. IDEFICS ===
|
| 21 |
+
idefics_proc = AutoProcessor.from_pretrained("HuggingFaceM4/idefics-9b-instruct")
|
| 22 |
+
idefics_model = IdeficsForConditionalGeneration.from_pretrained(
|
| 23 |
+
"HuggingFaceM4/idefics-9b-instruct", torch_dtype=torch.float16, device_map="auto"
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# === 4. Fuyu ===
|
| 27 |
+
fuyu_proc = AutoProcessor.from_pretrained("adept/fuyu-8b")
|
| 28 |
+
fuyu_model = FuyuForCausalLM.from_pretrained(
|
| 29 |
+
"adept/fuyu-8b", torch_dtype=torch.float16, device_map="auto"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# === 5. Gabungan Multi-Model Handler ===
|
| 33 |
+
def smart_respond(message, history, image=None):
|
| 34 |
+
if image:
|
| 35 |
+
results = []
|
| 36 |
+
|
| 37 |
+
# — LLaVA
|
| 38 |
+
try:
|
| 39 |
+
inputs = llava_proc(text=message, images=image, return_tensors="pt").to("cuda")
|
| 40 |
+
output = llava_model.generate(**inputs, max_new_tokens=512)
|
| 41 |
+
llava_reply = llava_proc.decode(output[0], skip_special_tokens=True)
|
| 42 |
+
results.append(f"🦙 **LLaVA 1.6**:\n{llava_reply}")
|
| 43 |
+
except Exception as e:
|
| 44 |
+
results.append(f"LLaVA error: {e}")
|
| 45 |
+
|
| 46 |
+
# — IDEFICS
|
| 47 |
+
try:
|
| 48 |
+
prompt = [{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": message}]}]
|
| 49 |
+
inputs = idefics_proc(prompt, images=[image], return_tensors="pt").to("cuda")
|
| 50 |
+
output = idefics_model.generate(**inputs, max_new_tokens=512)
|
| 51 |
+
idefics_reply = idefics_proc.decode(output[0], skip_special_tokens=True)
|
| 52 |
+
results.append(f"📄 **IDEFICS**:\n{idefics_reply}")
|
| 53 |
+
except Exception as e:
|
| 54 |
+
results.append(f"IDEFICS error: {e}")
|
| 55 |
+
|
| 56 |
+
# — Fuyu
|
| 57 |
+
try:
|
| 58 |
+
fuyu_inputs = fuyu_proc(images=image, text=message, return_tensors="pt").to("cuda")
|
| 59 |
+
output = fuyu_model.generate(**fuyu_inputs, max_new_tokens=512)
|
| 60 |
+
fuyu_reply = fuyu_proc.decode(output[0], skip_special_tokens=True)
|
| 61 |
+
results.append(f"🧠 **Fuyu**:\n{fuyu_reply}")
|
| 62 |
+
except Exception as e:
|
| 63 |
+
results.append(f"Fuyu error: {e}")
|
| 64 |
+
|
| 65 |
+
yield "\n\n---\n\n".join(results)
|
| 66 |
+
|
| 67 |
+
else:
|
| 68 |
+
# === Chat teks via OpenChat ===
|
| 69 |
+
messages = [{"role": "system", "content": "Elaina adalah AI ramah dalam bahasa Indonesia."}]
|
| 70 |
+
for user, bot in history:
|
| 71 |
+
if user: messages.append({"role": "user", "content": user})
|
| 72 |
+
if bot: messages.append({"role": "assistant", "content": bot})
|
| 73 |
+
messages.append({"role": "user", "content": message})
|
| 74 |
+
|
| 75 |
+
response = ""
|
| 76 |
+
for chunk in chat_client.chat_completion(messages, max_tokens=512, stream=True):
|
| 77 |
+
token = chunk.choices[0].delta.content
|
| 78 |
+
response += token
|
| 79 |
+
yield response
|
| 80 |
+
|
| 81 |
+
# === 6. Gradio Interface (ChatGPT-style) ===
|
| 82 |
+
with gr.Blocks() as demo:
|
| 83 |
+
gr.Markdown("## 🤖 Elaina AI — Teks + Gambar (4 Model)")
|
| 84 |
+
chatbot = gr.Chatbot()
|
| 85 |
+
state = gr.State([])
|
| 86 |
+
|
| 87 |
+
with gr.Row():
|
| 88 |
+
msg = gr.Textbox(placeholder="Ketik pesan kamu...", scale=4)
|
| 89 |
+
img = gr.Image(type="pil", label="(Opsional) Tambahkan gambar")
|
| 90 |
+
|
| 91 |
+
btn = gr.Button("Kirim")
|
| 92 |
+
|
| 93 |
+
def user_submit(message, image, history):
|
| 94 |
+
history = history or []
|
| 95 |
+
history.append((message, None))
|
| 96 |
+
return "", history, image
|
| 97 |
+
|
| 98 |
+
def bot_response(history, image):
|
| 99 |
+
user_msg = history[-1][0]
|
| 100 |
+
for result in smart_respond(user_msg, history[:-1], image):
|
| 101 |
+
yield history[:-1] + [(user_msg, result)]
|
| 102 |
+
|
| 103 |
+
btn.click(user_submit, [msg, img, state], [msg, state, img]) \
|
| 104 |
+
.then(bot_response, state, chatbot) \
|
| 105 |
+
.then(lambda x: x, chatbot, state)
|
| 106 |
+
|
| 107 |
+
# === 7. Jalankan ===
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
demo.launch()
|