Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,421 +1,311 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
from diffusers import DiffusionPipeline
|
| 6 |
-
import spaces
|
| 7 |
-
# κΈ°λ³Έ μ€μ
|
| 8 |
-
dtype = torch.bfloat16
|
| 9 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
torch_dtype=dtype
|
| 15 |
-
).to(device)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
"
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 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 |
-
"title": "Creative Process",
|
| 63 |
-
"prompt": """A handrawn colorful mind map diagram, artistic style, watercolor effects, flowing connections.
|
| 64 |
-
CREATIVITY
|
| 65 |
-
βββ INSPIRATION [Constellation Stars ~60px]
|
| 66 |
-
β βββ NATURE [Organic Patterns]
|
| 67 |
-
β βββ CULTURE [Global Icons]
|
| 68 |
-
βββ IDEATION [Floating Bubbles ~50px]
|
| 69 |
-
β βββ BRAINSTORM [Thunder Cloud]
|
| 70 |
-
β βββ REFINEMENT [Diamond Polish]
|
| 71 |
-
βββ EXECUTION [Artist Tools ~45px]
|
| 72 |
-
β βββ TECHNIQUE [Skilled Hands]
|
| 73 |
-
β βββ MEDIUM [Palette Mix]
|
| 74 |
-
βββ PRESENTATION
|
| 75 |
-
βββ GALLERY [Frame Display]
|
| 76 |
-
βββ FEEDBACK [Echo Ripples]""",
|
| 77 |
-
"width": 1024,
|
| 78 |
-
"height": 1024
|
| 79 |
-
},
|
| 80 |
-
{
|
| 81 |
-
"title": "Future Cities",
|
| 82 |
-
"prompt": """A handrawn colorful mind map diagram, futuristic style, holographic elements, sustainable themes.
|
| 83 |
-
SMART CITY
|
| 84 |
-
βββ MOBILITY [Hover Transport ~60px]
|
| 85 |
-
β βββ AUTONOMOUS [Self-Driving]
|
| 86 |
-
β βββ CONNECTED [Network Grid]
|
| 87 |
-
βββ ENERGY [Solar Crystal ~55px]
|
| 88 |
-
β βββ RENEWABLE [Green Power]
|
| 89 |
-
β βββ STORAGE [Battery Hub]
|
| 90 |
-
βββ LIVING [Eco Building ~50px]
|
| 91 |
-
β βββ VERTICAL [Sky Gardens]
|
| 92 |
-
β βββ COMMUNITY [People Connect]
|
| 93 |
-
βββ INFRASTRUCTURE
|
| 94 |
-
βββ AI GRID [Neural City]
|
| 95 |
-
βββ ECO SYSTEM [Nature Tech]""",
|
| 96 |
-
"width": 1024,
|
| 97 |
-
"height": 1024
|
| 98 |
-
},
|
| 99 |
-
{
|
| 100 |
-
"title": "Health Evolution",
|
| 101 |
-
"prompt": """A handrawn colorful mind map diagram, medical style, DNA helix patterns, wellness focus.
|
| 102 |
-
HEALTH 3.0
|
| 103 |
-
βββ PREVENTION [Shield DNA ~60px]
|
| 104 |
-
β βββ LIFESTYLE [Activity Pulse]
|
| 105 |
-
β βββ MONITORING [Health Watch]
|
| 106 |
-
βββ TREATMENT [Caduceus Tech ~55px]
|
| 107 |
-
β βββ PERSONALIZED [DNA Code]
|
| 108 |
-
β βββ REGENERATIVE [Cell Renew]
|
| 109 |
-
βββ ENHANCEMENT [Upgrade Spiral ~50px]
|
| 110 |
-
β βοΏ½οΏ½β COGNITIVE [Brain Boost]
|
| 111 |
-
β βββ PHYSICAL [Body Optimize]
|
| 112 |
-
βββ INTEGRATION
|
| 113 |
-
βββ AI HEALTH [Smart Doctor]
|
| 114 |
-
βββ COMMUNITY [Global Care]""",
|
| 115 |
-
"width": 1024,
|
| 116 |
-
"height": 1024
|
| 117 |
-
},
|
| 118 |
-
{
|
| 119 |
-
"title": "Space Exploration",
|
| 120 |
-
"prompt": """A handrawn colorful mind map diagram, cosmic style, star field background, planetary elements.
|
| 121 |
-
SPACE FRONTIER
|
| 122 |
-
βββ DISCOVERY [Telescope Array ~60px]
|
| 123 |
-
β βββ MAPPING [Star Charts]
|
| 124 |
-
β βββ ANALYSIS [Data Stream]
|
| 125 |
-
βββ TRAVEL [Rocket Launch ~55px]
|
| 126 |
-
β βββ PROPULSION [Energy Core]
|
| 127 |
-
β βββ NAVIGATION [Space Map]
|
| 128 |
-
βββ COLONIZATION [Dome City ~50px]
|
| 129 |
-
β βββ HABITATS [Life Sphere]
|
| 130 |
-
β βββ RESOURCES [Mine Extract]
|
| 131 |
-
βββ RESEARCH
|
| 132 |
-
βββ ASTROBIOLOGY [Life Search]
|
| 133 |
-
βββ PHYSICS [Space Time]""",
|
| 134 |
-
"width": 1024,
|
| 135 |
-
"height": 1024
|
| 136 |
-
},
|
| 137 |
-
{
|
| 138 |
-
"title": "Ocean Innovation",
|
| 139 |
-
"prompt": """A handrawn colorful mind map diagram, marine style, wave patterns, aqua themes.
|
| 140 |
-
OCEAN TECH
|
| 141 |
-
βββ EXPLORATION [Deep Submersible ~60px]
|
| 142 |
-
β βββ MAPPING [Sonar Wave]
|
| 143 |
-
β βββ RESEARCH [Lab Bubble]
|
| 144 |
-
βββ CONSERVATION [Marine Life ~55px]
|
| 145 |
-
β βββ PROTECTION [Reef Shield]
|
| 146 |
-
β βββ RESTORATION [Growth Core]
|
| 147 |
-
βββ HARVESTING [Sustainable Net ~50px]
|
| 148 |
-
β βββ ENERGY [Wave Power]
|
| 149 |
-
β βββ RESOURCES [Bio Extract]
|
| 150 |
-
βββ MONITORING
|
| 151 |
-
βββ AI SYSTEMS [Smart Sensors]
|
| 152 |
-
βββ ECOLOGY [Life Web]""",
|
| 153 |
-
"width": 1024,
|
| 154 |
-
"height": 1024
|
| 155 |
-
},
|
| 156 |
-
{
|
| 157 |
-
"title": "Quantum Computing",
|
| 158 |
-
"prompt": """A handrawn colorful mind map diagram, quantum style, wave-particle duality, matrix patterns.
|
| 159 |
-
QUANTUM TECH
|
| 160 |
-
βββ COMPUTATION [Qubit Matrix ~60px]
|
| 161 |
-
β βββ PROCESSING [Wave Function]
|
| 162 |
-
β βββ ALGORITHMS [Code Quantum]
|
| 163 |
-
βββ APPLICATIONS [Use Cases ~55px]
|
| 164 |
-
β βββ SIMULATION [Model World]
|
| 165 |
-
β βββ OPTIMIZATION [Peak Find]
|
| 166 |
-
βββ INFRASTRUCTURE [Q-Hardware ~50px]
|
| 167 |
-
β βββ CONTROL [Pulse Shape]
|
| 168 |
-
β βββ COOLING [Zero Point]
|
| 169 |
-
βββ DEVELOPMENT
|
| 170 |
-
βββ SOFTWARE [Q-Code Web]
|
| 171 |
-
βββ INTEGRATION [Classical Bridge]""",
|
| 172 |
-
"width": 1024,
|
| 173 |
-
"height": 1024
|
| 174 |
-
},
|
| 175 |
-
{
|
| 176 |
-
"title": "Bio Engineering",
|
| 177 |
-
"prompt": """A handrawn colorful mind map diagram, biological style, DNA patterns, organic flow.
|
| 178 |
-
BIOTECH
|
| 179 |
-
βββ GENETICS [DNA Helix ~60px]
|
| 180 |
-
β βββ EDITING [CRISPR Tool]
|
| 181 |
-
β βββ SYNTHESIS [Gene Build]
|
| 182 |
-
βββ APPLICATIONS [Lab Array ~55px]
|
| 183 |
-
β βββ MEDICINE [Heal Cell]
|
| 184 |
-
β βββ AGRICULTURE [Grow Plus]
|
| 185 |
-
βββ PLATFORMS [Bio Factory ~50px]
|
| 186 |
-
β βββ SENSORS [Live Detect]
|
| 187 |
-
β βββ PROCESSORS [Cell Compute]
|
| 188 |
-
βββ INTEGRATION
|
| 189 |
-
βββ AI BIOLOGY [Smart Life]
|
| 190 |
-
βββ ECOSYSTEM [Nature Net]""",
|
| 191 |
-
"width": 1024,
|
| 192 |
-
"height": 1024
|
| 193 |
-
},
|
| 194 |
-
{
|
| 195 |
-
"title": "AI Evolution",
|
| 196 |
-
"prompt": """A handrawn colorful mind map diagram, neural network style, digital patterns, intelligence flow.
|
| 197 |
-
AI FUTURE
|
| 198 |
-
βββ COGNITION [Brain Network ~60px]
|
| 199 |
-
β βββ LEARNING [Growth Path]
|
| 200 |
-
β βββ REASONING [Logic Tree]
|
| 201 |
-
βββ PERCEPTION [Sensor Array ~55px]
|
| 202 |
-
β βββ VISION [Eye Matrix]
|
| 203 |
-
β βββ LANGUAGE [Word Web]
|
| 204 |
-
βββ INTERACTION [Connect Hub ~50px]
|
| 205 |
-
β βββ HUMAN [Bridge Link]
|
| 206 |
-
β βββ MACHINE [Code Path]
|
| 207 |
-
βββ EVOLUTION
|
| 208 |
-
βββ CONSCIOUSNESS [Mind Spark]
|
| 209 |
-
βββ CREATIVITY [Art Core]""",
|
| 210 |
-
"width": 1024,
|
| 211 |
-
"height": 1024
|
| 212 |
-
}
|
| 213 |
-
]
|
| 214 |
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
height=height,
|
| 230 |
-
num_inference_steps=num_inference_steps,
|
| 231 |
-
generator=generator,
|
| 232 |
-
guidance_scale=0.0
|
| 233 |
-
).images[0]
|
| 234 |
-
return image, seed
|
| 235 |
|
| 236 |
-
|
| 237 |
-
css = """
|
| 238 |
.container {
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
height: 100%;
|
| 242 |
}
|
| 243 |
|
| 244 |
-
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
padding: 20px;
|
| 247 |
-
border-right: 2px solid #eee;
|
| 248 |
-
max-width: 800px; /* μ¦κ°λ μ΅λ λλΉ */
|
| 249 |
}
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
|
|
|
| 256 |
}
|
| 257 |
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
padding:
|
| 262 |
-
|
| 263 |
-
font-weight: bold;
|
| 264 |
-
background: linear-gradient(90deg, #f0f0f0 0%, #ffffff 100%);
|
| 265 |
-
border-bottom: 3px solid #ddd;
|
| 266 |
-
margin-bottom: 30px;
|
| 267 |
}
|
| 268 |
|
| 269 |
-
.
|
| 270 |
-
|
| 271 |
-
color:
|
| 272 |
-
margin-
|
| 273 |
}
|
| 274 |
|
| 275 |
-
.
|
| 276 |
-
background:
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
| 280 |
-
margin-bottom: 20px;
|
| 281 |
-
width: 100%; /* μ 체 λλΉ μ¬μ© */
|
| 282 |
}
|
| 283 |
|
| 284 |
-
/*
|
| 285 |
-
.
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
padding: 12px !important;
|
| 291 |
}
|
| 292 |
|
| 293 |
-
.
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
margin: 10px 0;
|
| 297 |
-
border-radius: 8px;
|
| 298 |
-
box-shadow: 0 2px 5px rgba(0,0,0,0.05);
|
| 299 |
}
|
| 300 |
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
|
|
|
|
|
|
| 305 |
}
|
| 306 |
|
| 307 |
-
/*
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
}
|
| 312 |
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
}
|
| 317 |
|
| 318 |
-
/*
|
| 319 |
-
|
| 320 |
-
|
|
|
|
| 321 |
}
|
| 322 |
-
"""
|
| 323 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
<div class="subtitle">Create beautiful hand-drawn style diagrams using FLUX AI</div>
|
| 331 |
-
""")
|
| 332 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
|
| 335 |
-
with gr.Row(
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
)
|
| 345 |
-
|
| 346 |
-
run_button = gr.Button("Generate Diagram", variant="primary")
|
| 347 |
-
result = gr.Image(label="Generated Diagram")
|
| 348 |
|
| 349 |
-
with gr.
|
| 350 |
-
|
| 351 |
-
label="
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
step=1,
|
| 355 |
-
value=0,
|
| 356 |
)
|
| 357 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
maximum=MAX_IMAGE_SIZE,
|
| 364 |
-
step=32,
|
| 365 |
-
value=1024,
|
| 366 |
-
)
|
| 367 |
-
height = gr.Slider(
|
| 368 |
-
label="Height",
|
| 369 |
-
minimum=256,
|
| 370 |
-
maximum=MAX_IMAGE_SIZE,
|
| 371 |
-
step=32,
|
| 372 |
-
value=1024,
|
| 373 |
-
)
|
| 374 |
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
outputs=[prompt, width, height]
|
| 403 |
-
)
|
| 404 |
-
|
| 405 |
-
gr.on(
|
| 406 |
-
triggers=[run_button.click, prompt.submit],
|
| 407 |
-
fn=infer,
|
| 408 |
-
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
|
| 409 |
-
outputs=[result, seed]
|
| 410 |
)
|
| 411 |
|
| 412 |
-
|
| 413 |
-
if __name__ == "__main__":
|
| 414 |
-
demo.queue()
|
| 415 |
-
demo.launch(
|
| 416 |
-
server_name="0.0.0.0",
|
| 417 |
-
server_port=7860,
|
| 418 |
-
share=False,
|
| 419 |
-
show_error=True,
|
| 420 |
-
debug=True
|
| 421 |
-
)
|
|
|
|
| 1 |
+
# 2) The actual app
|
| 2 |
+
import os
|
| 3 |
+
from getpass import getpass
|
| 4 |
+
from openai import OpenAI
|
| 5 |
import gradio as gr
|
| 6 |
+
import requests
|
| 7 |
+
import json
|
| 8 |
+
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# βββ Configure your OpenRouter key βββ
|
| 11 |
+
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
|
| 12 |
+
BSEARCH_API = os.getenv("BSEARCH_API")
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Check if the API key was retrieved
|
| 15 |
+
if not OPENROUTER_API_KEY:
|
| 16 |
+
print("Error: OPENROUTER_API_KEY not found in environment.")
|
| 17 |
+
print("Please set your API key in the environment as 'OPENROUTER_API_KEY'.")
|
| 18 |
+
else:
|
| 19 |
+
client = OpenAI(
|
| 20 |
+
base_url="https://openrouter.ai/api/v1",
|
| 21 |
+
api_key=OPENROUTER_API_KEY,
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
# Brave Search function
|
| 25 |
+
def brave_search(query):
|
| 26 |
+
"""Perform a web search using Brave Search API."""
|
| 27 |
+
if not BSEARCH_API:
|
| 28 |
+
return "Error: BSEARCH_API not found in environment. Please set your Brave Search API key."
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
headers = {
|
| 32 |
+
"Accept": "application/json",
|
| 33 |
+
"X-Subscription-Token": BSEARCH_API
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# Brave Search API endpoint
|
| 37 |
+
url = "https://api.search.brave.com/res/v1/web/search"
|
| 38 |
+
params = {
|
| 39 |
+
"q": query,
|
| 40 |
+
"count": 5 # Number of results to return
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
response = requests.get(url, headers=headers, params=params)
|
| 44 |
+
response.raise_for_status()
|
| 45 |
+
|
| 46 |
+
data = response.json()
|
| 47 |
+
|
| 48 |
+
# Format the search results
|
| 49 |
+
results = []
|
| 50 |
+
if "web" in data and "results" in data["web"]:
|
| 51 |
+
for idx, result in enumerate(data["web"]["results"][:5], 1):
|
| 52 |
+
title = result.get("title", "No title")
|
| 53 |
+
url = result.get("url", "")
|
| 54 |
+
description = result.get("description", "No description")
|
| 55 |
+
results.append(f"{idx}. **{title}**\n URL: {url}\n {description}\n")
|
| 56 |
+
|
| 57 |
+
if results:
|
| 58 |
+
return "π **Web Search Results:**\n\n" + "\n".join(results)
|
| 59 |
+
else:
|
| 60 |
+
return "No search results found."
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
return f"Search error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
def openrouter_chat(user_message, history, use_web_search):
|
| 66 |
+
"""Send user_message and history to mistralai/devstral-small:free and append to history."""
|
| 67 |
+
history = history or []
|
| 68 |
+
|
| 69 |
+
# If web search is enabled, perform search first
|
| 70 |
+
search_context = ""
|
| 71 |
+
if use_web_search and user_message.strip():
|
| 72 |
+
search_results = brave_search(user_message)
|
| 73 |
+
search_context = f"\n\n{search_results}\n\nBased on the above search results, please answer the following question:\n"
|
| 74 |
+
# Add search results to history as a system message
|
| 75 |
+
history.append(("π Web Search Query", user_message))
|
| 76 |
+
history.append(("π Search Results", search_results))
|
| 77 |
+
|
| 78 |
+
# Build the messages list from the history and the current user message
|
| 79 |
+
messages_for_api = []
|
| 80 |
+
|
| 81 |
+
# Add system message if web search was used
|
| 82 |
+
if search_context:
|
| 83 |
+
messages_for_api.append({
|
| 84 |
+
"role": "system",
|
| 85 |
+
"content": "You are a helpful assistant. When web search results are provided, incorporate them into your response to give accurate and up-to-date information."
|
| 86 |
+
})
|
| 87 |
+
|
| 88 |
+
for human_message, ai_message in history[:-2] if use_web_search else history: # Exclude search entries from API messages
|
| 89 |
+
if not human_message.startswith("π") and not human_message.startswith("π"):
|
| 90 |
+
messages_for_api.append({"role": "user", "content": human_message})
|
| 91 |
+
if ai_message is not None:
|
| 92 |
+
messages_for_api.append({"role": "assistant", "content": ai_message})
|
| 93 |
+
|
| 94 |
+
# Add the current user message with search context if applicable
|
| 95 |
+
current_message = search_context + user_message if search_context else user_message
|
| 96 |
+
messages_for_api.append({"role": "user", "content": current_message})
|
| 97 |
+
|
| 98 |
+
try:
|
| 99 |
+
# Call the model with the mistralai/Devstral-Small-2505 for full conversation history
|
| 100 |
+
resp = client.chat.completions.create(
|
| 101 |
+
model="mistralai/devstral-small:free",
|
| 102 |
+
messages=messages_for_api,
|
| 103 |
+
# you can tweak max_tokens, temperature, etc. here
|
| 104 |
+
)
|
| 105 |
+
bot_reply = resp.choices[0].message.content
|
| 106 |
+
# Append the user message and bot reply to the history for Gradio display
|
| 107 |
+
history.append((user_message, bot_reply))
|
| 108 |
+
except Exception as e:
|
| 109 |
+
# Handle potential errors and append an error message to the history
|
| 110 |
+
history.append((user_message, f"Error: {e}"))
|
| 111 |
+
|
| 112 |
+
return history, ""
|
| 113 |
|
| 114 |
+
# Enhanced CSS with gradient background and visual improvements
|
| 115 |
+
custom_css = """
|
| 116 |
+
/* Gradient background */
|
| 117 |
+
.gradio-container {
|
| 118 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 25%, #f093fb 50%, #4facfe 75%, #00f2fe 100%);
|
| 119 |
+
min-height: 100vh;
|
| 120 |
+
padding: 20px;
|
| 121 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
/* Main container styling */
|
|
|
|
| 124 |
.container {
|
| 125 |
+
max-width: 1200px;
|
| 126 |
+
margin: 0 auto;
|
|
|
|
| 127 |
}
|
| 128 |
|
| 129 |
+
/* Chat container with glassmorphism effect */
|
| 130 |
+
#component-0 {
|
| 131 |
+
background: rgba(255, 255, 255, 0.1);
|
| 132 |
+
backdrop-filter: blur(10px);
|
| 133 |
+
border-radius: 20px;
|
| 134 |
+
border: 1px solid rgba(255, 255, 255, 0.2);
|
| 135 |
+
box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
|
| 136 |
padding: 20px;
|
|
|
|
|
|
|
| 137 |
}
|
| 138 |
|
| 139 |
+
/* Chatbot styling */
|
| 140 |
+
.chatbot {
|
| 141 |
+
background: rgba(255, 255, 255, 0.9) !important;
|
| 142 |
+
border-radius: 15px !important;
|
| 143 |
+
border: none !important;
|
| 144 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1) !important;
|
| 145 |
}
|
| 146 |
|
| 147 |
+
/* Message bubbles */
|
| 148 |
+
.message {
|
| 149 |
+
border-radius: 10px !important;
|
| 150 |
+
padding: 12px 16px !important;
|
| 151 |
+
margin: 8px 0 !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
}
|
| 153 |
|
| 154 |
+
.user {
|
| 155 |
+
background-color: #667eea !important;
|
| 156 |
+
color: white !important;
|
| 157 |
+
margin-left: 20% !important;
|
| 158 |
}
|
| 159 |
|
| 160 |
+
.bot {
|
| 161 |
+
background-color: #f0f0f0 !important;
|
| 162 |
+
color: #333 !important;
|
| 163 |
+
margin-right: 20% !important;
|
|
|
|
|
|
|
|
|
|
| 164 |
}
|
| 165 |
|
| 166 |
+
/* Input styling */
|
| 167 |
+
.textbox {
|
| 168 |
+
border-radius: 10px !important;
|
| 169 |
+
border: 2px solid #667eea !important;
|
| 170 |
+
background: rgba(255, 255, 255, 0.95) !important;
|
| 171 |
+
font-size: 16px !important;
|
|
|
|
| 172 |
}
|
| 173 |
|
| 174 |
+
.textbox:focus {
|
| 175 |
+
border-color: #764ba2 !important;
|
| 176 |
+
box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2) !important;
|
|
|
|
|
|
|
|
|
|
| 177 |
}
|
| 178 |
|
| 179 |
+
/* Checkbox styling */
|
| 180 |
+
.checkbox-group {
|
| 181 |
+
background: rgba(255, 255, 255, 0.8) !important;
|
| 182 |
+
border-radius: 10px !important;
|
| 183 |
+
padding: 10px !important;
|
| 184 |
+
margin: 10px 0 !important;
|
| 185 |
}
|
| 186 |
|
| 187 |
+
/* Button styling */
|
| 188 |
+
button {
|
| 189 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 190 |
+
color: white !important;
|
| 191 |
+
border: none !important;
|
| 192 |
+
border-radius: 10px !important;
|
| 193 |
+
padding: 10px 20px !important;
|
| 194 |
+
font-weight: bold !important;
|
| 195 |
+
transition: all 0.3s ease !important;
|
| 196 |
}
|
| 197 |
|
| 198 |
+
button:hover {
|
| 199 |
+
transform: translateY(-2px) !important;
|
| 200 |
+
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3) !important;
|
| 201 |
}
|
| 202 |
|
| 203 |
+
/* Title styling */
|
| 204 |
+
h1, h2 {
|
| 205 |
+
color: white !important;
|
| 206 |
+
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3) !important;
|
| 207 |
}
|
|
|
|
| 208 |
|
| 209 |
+
/* Markdown styling */
|
| 210 |
+
.markdown-text {
|
| 211 |
+
color: white !important;
|
| 212 |
+
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3) !important;
|
| 213 |
+
}
|
| 214 |
|
| 215 |
+
/* Web search results styling */
|
| 216 |
+
.message:has(.search-results) {
|
| 217 |
+
background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%) !important;
|
| 218 |
+
border-left: 4px solid #ff6b6b !important;
|
| 219 |
+
}
|
|
|
|
|
|
|
| 220 |
|
| 221 |
+
/* Responsive design */
|
| 222 |
+
@media (max-width: 768px) {
|
| 223 |
+
.user {
|
| 224 |
+
margin-left: 10% !important;
|
| 225 |
+
}
|
| 226 |
+
.bot {
|
| 227 |
+
margin-right: 10% !important;
|
| 228 |
+
}
|
| 229 |
+
}
|
| 230 |
+
"""
|
| 231 |
|
| 232 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 233 |
+
gr.Markdown(
|
| 234 |
+
"""
|
| 235 |
+
<div style="text-align: center; padding: 20px;">
|
| 236 |
+
<h1 style="font-size: 3em; margin-bottom: 10px;">π¦π AI Chat Assistant</h1>
|
| 237 |
+
<h2 style="font-size: 1.5em; opacity: 0.9;">Powered by Devstral-Small with Brave Search Integration</h2>
|
| 238 |
+
</div>
|
| 239 |
+
""",
|
| 240 |
+
elem_id="header"
|
| 241 |
+
)
|
| 242 |
|
| 243 |
+
with gr.Row():
|
| 244 |
+
with gr.Column(scale=4):
|
| 245 |
+
chatbot = gr.Chatbot(
|
| 246 |
+
label="π¬ Chat History",
|
| 247 |
+
height=500,
|
| 248 |
+
elem_classes=["chatbot"],
|
| 249 |
+
bubble_full_width=False
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
with gr.Row():
|
| 253 |
+
msg_in = gr.Textbox(
|
| 254 |
+
placeholder="Type your question here⦠(Press Enter to send)",
|
| 255 |
+
label="βοΈ Your Message",
|
| 256 |
+
scale=4,
|
| 257 |
+
lines=2
|
| 258 |
)
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
+
with gr.Row():
|
| 261 |
+
use_web_search = gr.Checkbox(
|
| 262 |
+
label="π Enable Web Search",
|
| 263 |
+
value=True,
|
| 264 |
+
info="Search the web for current information before answering"
|
|
|
|
|
|
|
| 265 |
)
|
| 266 |
+
clear_btn = gr.Button("ποΈ Clear Chat", size="sm")
|
| 267 |
+
|
| 268 |
+
with gr.Column(scale=1):
|
| 269 |
+
gr.Markdown(
|
| 270 |
+
"""
|
| 271 |
+
### π Features
|
| 272 |
+
- π¬ Real-time chat with AI
|
| 273 |
+
- π Web search integration
|
| 274 |
+
- π Conversation history
|
| 275 |
+
- π¨ Beautiful gradient UI
|
| 276 |
|
| 277 |
+
### π§ Tips
|
| 278 |
+
- Enable web search for current events
|
| 279 |
+
- Ask follow-up questions
|
| 280 |
+
- Clear chat to start fresh
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
+
### β‘ Shortcuts
|
| 283 |
+
- Enter: Send message
|
| 284 |
+
- Shift+Enter: New line
|
| 285 |
+
""",
|
| 286 |
+
elem_classes=["markdown-text"]
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
# Event handlers
|
| 290 |
+
msg_in.submit(
|
| 291 |
+
openrouter_chat,
|
| 292 |
+
inputs=[msg_in, chatbot, use_web_search],
|
| 293 |
+
outputs=[chatbot, msg_in]
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg_in])
|
| 297 |
+
|
| 298 |
+
# Add example queries
|
| 299 |
+
gr.Examples(
|
| 300 |
+
examples=[
|
| 301 |
+
["What's the latest news about AI?", True],
|
| 302 |
+
["Explain quantum computing in simple terms", False],
|
| 303 |
+
["What's the weather like today?", True],
|
| 304 |
+
["Write a Python function to sort a list", False],
|
| 305 |
+
["What are the current stock market trends?", True]
|
| 306 |
+
],
|
| 307 |
+
inputs=[msg_in, use_web_search],
|
| 308 |
+
label="π‘ Example Queries"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
)
|
| 310 |
|
| 311 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|