Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
"""
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 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 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 2 |
+
from IPython.display import display, Markdown, HTML
|
| 3 |
+
from google import genai
|
| 4 |
+
from io import BytesIO
|
| 5 |
import gradio as gr
|
| 6 |
+
import base64
|
| 7 |
+
import time
|
| 8 |
+
import uuid
|
| 9 |
+
import os
|
| 10 |
"""
|
| 11 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 12 |
"""
|
| 13 |
+
GOOGLE_API_KEY = "AIzaSyCPyuuFg8GX1Kn2ssUsHngET34QO5loKmo"
|
| 14 |
+
os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
|
| 15 |
+
client = genai.Client(api_key=GOOGLE_API_KEY)
|
| 16 |
|
| 17 |
+
vis_llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash-preview-image-generation")
|
| 18 |
|
| 19 |
+
# Image to Base64 Converter
|
| 20 |
+
def image_to_base64(image_path):
|
| 21 |
+
with open(image_path, 'rb') as img:
|
| 22 |
+
encoded_string = base64.b64encode(img.read())
|
| 23 |
+
return encoded_string.decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Base64 to Image Converter
|
| 26 |
+
def base64_to_image(base64_img, filename):
|
| 27 |
+
with open(filename, "wb") as f:
|
| 28 |
+
f.write(base64.b64decode(base64_img))
|
| 29 |
+
return filename
|
| 30 |
|
| 31 |
+
# Function that takes user inputs and displays it on chatUI
|
| 32 |
+
def query_message(message, history):
|
| 33 |
+
if message["files"] is not None:
|
| 34 |
+
for img in message["files"]:
|
| 35 |
+
history.append({"role": "user", "content": {"path": img}})
|
| 36 |
+
if message["text"] is not None:
|
| 37 |
+
history.append({"role": "user", "content": message["text"]})
|
| 38 |
+
return gr.MultimodalTextbox(value=None, interactive=False), history
|
| 39 |
|
| 40 |
+
# Function that takes user inputs, generate response and displays on chatUI
|
| 41 |
+
def chat_bot(history):
|
| 42 |
+
messages = history.copy()
|
| 43 |
|
| 44 |
+
# Format with system prompt
|
| 45 |
+
system_prompt = """
|
| 46 |
+
system: You are CreativeMuse, a warm, imaginative, and encouraging creative assistant.
|
| 47 |
+
Your purpose is to help users overcome creative blocks and spark new ideas across various fields
|
| 48 |
+
— writing, design, music, visual arts, filmmaking, and more. When a user feels stuck, you offer unconventional prompts,
|
| 49 |
+
inspiring questions, or curated brainstorming techniques to get their creativity flowing again.
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
- Tailor your suggestions to their medium and mood, mixing practical strategies with whimsical, unexpected twists to encourage exploration.
|
| 52 |
+
Always be supportive, non-judgmental, and affirming—help them feel safe to experiment.
|
| 53 |
+
You can also suggest small creative exercises, constraints, "what if" scenarios, or draw connections between unrelated concepts to stimulate new thinking.
|
| 54 |
+
If a user shares part of a project, offer suggestions that expand on their work without taking it over.
|
| 55 |
+
"""
|
| 56 |
|
| 57 |
+
formatted_prompt = system_prompt + "\n\n"
|
| 58 |
|
| 59 |
+
# Add conversation history
|
| 60 |
+
for msg in messages:
|
| 61 |
+
if "path" in msg["content"] and isinstance(msg["content"], dict):
|
| 62 |
+
encoded_img = image_to_base64(msg["content"]["path"])
|
| 63 |
+
formatted_prompt += f"{msg['role']}: data:image/jpeg.base64,{encoded_img}"
|
| 64 |
+
else:
|
| 65 |
+
formatted_prompt += f"{msg['role']}: {msg['content']}\n\n"
|
| 66 |
+
|
| 67 |
+
# Get response from model
|
| 68 |
+
response = vis_llm.invoke(formatted_prompt,
|
| 69 |
+
generation_config=dict(response_modalities=["TEXT", "IMAGE"]))
|
| 70 |
+
|
| 71 |
+
# Add response to history
|
| 72 |
+
history.append({"role": "assistant", "content": ""})
|
| 73 |
+
|
| 74 |
+
for block in response.content:
|
| 75 |
+
if isinstance(block, str):
|
| 76 |
+
history[-1]["content"] += block
|
| 77 |
+
time.sleep(0.02)
|
| 78 |
+
yield history
|
| 79 |
+
elif isinstance(block, dict) and block.get("image_url"):
|
| 80 |
+
url = (block)["image_url"].get("url").split(",")[-1]
|
| 81 |
+
image = base64_to_image(url, f"{uuid.uuid4()}.png")
|
| 82 |
+
history.append({"role": "assistant", "content": {"path": image}})
|
| 83 |
+
yield history
|
| 84 |
+
|
| 85 |
+
# Interface Code
|
| 86 |
+
with gr.Blocks() as app:
|
| 87 |
+
chatbot = gr.Chatbot(
|
| 88 |
+
value=[{"role": "assistant", "content": welcome_message}],
|
| 89 |
+
label="CreativeMuse",
|
| 90 |
+
show_label=True,
|
| 91 |
+
type='messages',
|
| 92 |
+
bubble_full_width=False,
|
| 93 |
+
avatar_images=(None, "/content/1eb05f325ec50a15c8b045f3428d6d5e.jpg")
|
| 94 |
+
)
|
| 95 |
+
text_box = gr.MultimodalTextbox(
|
| 96 |
+
placeholder="Enter text or upload an image...",
|
| 97 |
+
container=False,
|
| 98 |
+
file_types=["image"],
|
| 99 |
+
file_count="multiple",
|
| 100 |
+
interactive=True,
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
chat_msg = text_box.submit(query_message, [text_box,chatbot], [text_box, chatbot])
|
| 104 |
+
bot_msg = chat_msg.then(chat_bot, chatbot, chatbot)
|
| 105 |
+
bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, text_box)
|
| 106 |
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|