Spaces:
Sleeping
Sleeping
Update.py
Browse files
app.py
CHANGED
|
@@ -1,204 +1,72 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import time
|
| 3 |
-
from typing import List, Tuple, Optional
|
| 4 |
-
import google.generativeai as genai
|
| 5 |
import gradio as gr
|
| 6 |
-
from
|
| 7 |
-
import tempfile
|
| 8 |
import os
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
image_height = int(image.height * IMAGE_WIDTH / image.width)
|
| 26 |
-
return image.resize((IMAGE_WIDTH, image_height))
|
| 27 |
-
|
| 28 |
-
def user(text_prompt: str, chatbot: List[Tuple[str, str]]):
|
| 29 |
-
return "", chatbot + [[text_prompt, None]]
|
| 30 |
-
|
| 31 |
-
def bot(
|
| 32 |
-
google_key: str,
|
| 33 |
-
image_prompt: Optional[Image.Image],
|
| 34 |
-
temperature: float,
|
| 35 |
-
max_output_tokens: int,
|
| 36 |
-
stop_sequences: str,
|
| 37 |
-
top_k: int,
|
| 38 |
-
top_p: float,
|
| 39 |
-
chatbot: List[Tuple[str, str]]
|
| 40 |
-
):
|
| 41 |
-
google_key = google_key or GOOGLE_API_KEY
|
| 42 |
-
if not google_key:
|
| 43 |
-
raise ValueError("GOOGLE_API_KEY is not set. Please set it up.")
|
| 44 |
-
|
| 45 |
-
text_prompt = chatbot[-1][0].strip() if chatbot[-1][0] else None
|
| 46 |
-
|
| 47 |
-
# Handle cases for text and/or image input
|
| 48 |
-
if not text_prompt and not image_prompt:
|
| 49 |
-
chatbot[-1][1] = "Prompt cannot be empty. Please provide input text or an image."
|
| 50 |
-
yield chatbot
|
| 51 |
-
return
|
| 52 |
-
elif image_prompt and not text_prompt:
|
| 53 |
-
# If only an image is provided
|
| 54 |
-
text_prompt = "Describe the image"
|
| 55 |
-
elif image_prompt and text_prompt:
|
| 56 |
-
# If both text and image are provided, combine them
|
| 57 |
-
text_prompt = f"{text_prompt}. Also, analyze the provided image."
|
| 58 |
-
|
| 59 |
-
# Configure the model
|
| 60 |
-
genai.configure(api_key=google_key)
|
| 61 |
-
generation_config = genai.types.GenerationConfig(
|
| 62 |
-
temperature=temperature,
|
| 63 |
-
max_output_tokens=max_output_tokens,
|
| 64 |
-
stop_sequences=preprocess_stop_sequences(stop_sequences),
|
| 65 |
-
top_k=top_k,
|
| 66 |
-
top_p=top_p,
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
# Prepare inputs
|
| 70 |
-
inputs = [text_prompt] if image_prompt is None else [text_prompt, preprocess_image(image_prompt)]
|
| 71 |
-
|
| 72 |
-
# Generate response
|
| 73 |
try:
|
| 74 |
-
response =
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
except Exception as e:
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
for
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
label="Response Length (Token Limit)",
|
| 118 |
-
info="Sets the maximum number of tokens in the output response."
|
| 119 |
-
)
|
| 120 |
-
stop_sequences_component = gr.Textbox(
|
| 121 |
-
label="Stop Sequences (Optional)",
|
| 122 |
-
placeholder="Enter stop sequences, e.g., STOP, END",
|
| 123 |
-
info="Specify sequences to stop the generation."
|
| 124 |
)
|
| 125 |
-
top_k_component = gr.Slider(
|
| 126 |
-
minimum=1,
|
| 127 |
-
maximum=40,
|
| 128 |
-
value=32,
|
| 129 |
-
step=1,
|
| 130 |
-
label="Top-K Sampling",
|
| 131 |
-
info="Limits token selection to the top K most probable tokens. Lower values produce conservative outputs."
|
| 132 |
-
)
|
| 133 |
-
top_p_component = gr.Slider(
|
| 134 |
-
minimum=0,
|
| 135 |
-
maximum=1,
|
| 136 |
-
value=1,
|
| 137 |
-
step=0.01,
|
| 138 |
-
label="Top-P Sampling",
|
| 139 |
-
info="Limits token selection to tokens with a cumulative probability up to P. Lower values produce conservative outputs."
|
| 140 |
-
)
|
| 141 |
-
example_scenarios = [
|
| 142 |
-
"Describe Multimodal AI",
|
| 143 |
-
"What are the difference between muliagent llm and multiagent system",
|
| 144 |
-
"Why it's difficult to intgrate multimodality in prompt"]
|
| 145 |
-
example_images = [["ex1.png"],["ex2.png"]]
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
# Gradio Interface
|
| 149 |
-
user_inputs = [text_prompt_component, chatbot_component]
|
| 150 |
-
bot_inputs = [
|
| 151 |
-
google_key_component,
|
| 152 |
-
image_prompt_component,
|
| 153 |
-
temperature_component,
|
| 154 |
-
max_output_tokens_component,
|
| 155 |
-
stop_sequences_component,
|
| 156 |
-
top_k_component,
|
| 157 |
-
top_p_component,
|
| 158 |
-
chatbot_component,
|
| 159 |
-
]
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
with gr.Blocks(theme="earneleh/paris") as demo:
|
| 163 |
-
gr.Markdown("<h1 style='font-size: 36px; font-weight: bold; font-family: Arial;'>Gemini 2.0 Multimodal Chatbot</h1>")
|
| 164 |
-
with gr.Row():
|
| 165 |
-
google_key_component.render()
|
| 166 |
-
with gr.Row():
|
| 167 |
-
chatbot_component.render()
|
| 168 |
-
with gr.Row():
|
| 169 |
-
with gr.Column(scale=0.5):
|
| 170 |
-
text_prompt_component.render()
|
| 171 |
-
with gr.Column(scale=0.5):
|
| 172 |
-
image_prompt_component.render()
|
| 173 |
-
with gr.Column(scale=0.5):
|
| 174 |
-
run_button_component.render()
|
| 175 |
-
with gr.Accordion("🧪Example Text 💬", open=False):
|
| 176 |
-
example_radio = gr.Radio(
|
| 177 |
-
choices=example_scenarios,
|
| 178 |
-
label="Example Queries",
|
| 179 |
-
info="Select an example query.")
|
| 180 |
-
# Debug callback
|
| 181 |
-
example_radio.change(
|
| 182 |
-
fn=lambda query: query if query else "No query selected.",
|
| 183 |
-
inputs=[example_radio],
|
| 184 |
-
outputs=[text_prompt_component])
|
| 185 |
-
# Custom examples section with blue styling
|
| 186 |
-
with gr.Accordion("🧪Example Image 🩻", open=False):
|
| 187 |
-
gr.Examples(
|
| 188 |
-
examples=example_images,
|
| 189 |
-
inputs=[image_prompt_component],
|
| 190 |
-
label="Example Figures",
|
| 191 |
-
)
|
| 192 |
-
with gr.Accordion("🛠️Customize", open=False):
|
| 193 |
-
temperature_component.render()
|
| 194 |
-
max_output_tokens_component.render()
|
| 195 |
-
stop_sequences_component.render()
|
| 196 |
-
top_k_component.render()
|
| 197 |
-
top_p_component.render()
|
| 198 |
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
).then(
|
| 202 |
-
fn=bot, inputs=bot_inputs, outputs=[chatbot_component]
|
| 203 |
-
)
|
| 204 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from groq import Groq
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 6 |
+
|
| 7 |
+
SYSTEM_PROMPT = """You are an expert in storyboarding. Provide structured and insightful responses to queries
|
| 8 |
+
about creating and refining storyboards"""
|
| 9 |
+
|
| 10 |
+
def respond(message, history, model, temperature, max_tokens):
|
| 11 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 12 |
+
|
| 13 |
+
for h in history:
|
| 14 |
+
messages.append({"role": "user", "content": h[0]})
|
| 15 |
+
if h[1]:
|
| 16 |
+
messages.append({"role": "assistant", "content": h[1]})
|
| 17 |
+
|
| 18 |
+
messages.append({"role": "user", "content": message})
|
| 19 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
+
response = client.chat.completions.create(
|
| 22 |
+
model=model,
|
| 23 |
+
messages=messages,
|
| 24 |
+
temperature=temperature,
|
| 25 |
+
max_completion_tokens=max_tokens,
|
| 26 |
+
)
|
| 27 |
+
return response.choices[0].message.content
|
| 28 |
except Exception as e:
|
| 29 |
+
return f"Error: {str(e)}"
|
| 30 |
+
|
| 31 |
+
# ChatInterface with additional inputs for parameters
|
| 32 |
+
demo = gr.ChatInterface(
|
| 33 |
+
fn=respond,
|
| 34 |
+
title="🎬 Storyboard Generator AI",
|
| 35 |
+
description="Create professional storyboards for films, animations, and more!",
|
| 36 |
+
additional_inputs=[
|
| 37 |
+
gr.Dropdown(
|
| 38 |
+
choices=[
|
| 39 |
+
"llama-3.3-70b-versatile",
|
| 40 |
+
"llama-3.1-8b-instant",
|
| 41 |
+
],
|
| 42 |
+
value="llama-3.3-70b-versatile",
|
| 43 |
+
label="Model",
|
| 44 |
+
info="Select the AI model to use"
|
| 45 |
+
),
|
| 46 |
+
gr.Slider(
|
| 47 |
+
minimum=0,
|
| 48 |
+
maximum=2,
|
| 49 |
+
value=0.9,
|
| 50 |
+
step=0.1,
|
| 51 |
+
label="Temperature",
|
| 52 |
+
info="Controls randomness. Lower = more focused, Higher = more creative"
|
| 53 |
+
),
|
| 54 |
+
gr.Slider(
|
| 55 |
+
minimum=256,
|
| 56 |
+
maximum=8192,
|
| 57 |
+
value=2048,
|
| 58 |
+
step=256,
|
| 59 |
+
label="Max Tokens",
|
| 60 |
+
info="Maximum length of the response"
|
| 61 |
+
),
|
| 62 |
+
],
|
| 63 |
+
examples=[
|
| 64 |
+
["Create a storyboard for a 30-second coffee commercial"],
|
| 65 |
+
["Generate a horror movie opening scene storyboard"],
|
| 66 |
+
["Design a storyboard for a romantic comedy meet-cute at a bookstore"],
|
| 67 |
+
],
|
| 68 |
+
theme="soft",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|