kshitijthakkar
commited on
Commit
Β·
8283a03
1
Parent(s):
db58867
ui changes and changed image model to dalle
Browse files- mcp_server.py +104 -101
- outage_odyssey_ui.py +3 -0
- prompts.yml +11 -11
mcp_server.py
CHANGED
@@ -498,114 +498,51 @@ def execute_incident_code(code: str) -> str:
|
|
498 |
# raise
|
499 |
|
500 |
|
501 |
-
|
502 |
-
# import openai
|
503 |
-
# @mcp.tool()
|
504 |
-
# def generate_image_with_dalle3(prompt: str) -> str:
|
505 |
-
# """
|
506 |
-
# Generates an image using the Dalle-3 via OpenAI API.
|
507 |
-
|
508 |
-
# This tool creates high-quality images from text prompts (max 2000 characters) using the Dalle-3
|
509 |
-
# model. The generated images are 1024x1024 pixels in PNG format and saved locally.
|
510 |
-
|
511 |
-
# Args:
|
512 |
-
# prompt (str): A descriptive text prompt for the image to generate. Be specific
|
513 |
-
# about details like title, characters, dialogue, style, composition, colors, and subject
|
514 |
-
# for best results.
|
515 |
-
|
516 |
-
# Returns:
|
517 |
-
# str: File path to the saved image (e.g., 'generated_images/comic_image_-620626766907224227.png'),
|
518 |
-
# or an error message if image generation fails.
|
519 |
-
|
520 |
-
# Raises:
|
521 |
-
# ValueError: If no image data is returned from the API.
|
522 |
-
# Exception: For other API or processing errors.
|
523 |
-
|
524 |
-
# Example:
|
525 |
-
# >>> generate_image_with_flux("A cartoon server room with smoke coming out")
|
526 |
-
# "generated_images/comic_image_123456789.png"
|
527 |
-
# """
|
528 |
-
# OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
529 |
-
# try:
|
530 |
-
# client = openai.OpenAI()
|
531 |
-
# response = client.images.generate(
|
532 |
-
# model="dall-e-3",
|
533 |
-
# prompt=prompt,
|
534 |
-
# n=1,
|
535 |
-
# size="1024x1024",
|
536 |
-
# quality="standard",
|
537 |
-
# response_format="url"
|
538 |
-
# )
|
539 |
-
# image_url = response.data[0].url
|
540 |
-
# logger.info(f'Generated image URL: {image_url}')
|
541 |
-
#
|
542 |
-
# # Download the image
|
543 |
-
# image_response = requests.get(image_url, timeout=30)
|
544 |
-
# image_response.raise_for_status()
|
545 |
-
# image = Image.open(BytesIO(image_response.content))
|
546 |
-
#
|
547 |
-
## Save to file
|
548 |
-
# temp_dir = Path("generated_images")
|
549 |
-
# temp_dir.mkdir(exist_ok=True)
|
550 |
-
# image_path = temp_dir / f"comic_image_{hash(prompt)}.png"
|
551 |
-
# image.save(image_path)
|
552 |
-
# normalized_path = str(image_path).replace("\\", "/")
|
553 |
-
# logger.info(f"Image saved to: {normalized_path}")
|
554 |
-
|
555 |
-
# return normalized_path
|
556 |
-
#
|
557 |
-
|
558 |
@mcp.tool()
|
559 |
-
def
|
560 |
"""
|
561 |
-
|
562 |
|
563 |
-
|
564 |
-
|
565 |
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
|
|
583 |
try:
|
584 |
-
|
585 |
-
client = OpenAI(
|
586 |
-
base_url="https://api.studio.nebius.com/v1/",
|
587 |
-
api_key=os.environ.get("NEBIUS_API_KEY")
|
588 |
-
)
|
589 |
response = client.images.generate(
|
590 |
-
model="
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
"negative_prompt": "",
|
598 |
-
"seed": -1
|
599 |
-
},
|
600 |
-
prompt=user_prompt
|
601 |
)
|
602 |
-
|
603 |
-
|
604 |
-
raise ValueError("No base64 image data returned in response")
|
605 |
|
606 |
-
#
|
607 |
-
|
608 |
-
|
|
|
609 |
|
610 |
# Save to file
|
611 |
temp_dir = Path("generated_images")
|
@@ -615,14 +552,80 @@ def generate_image_with_flux(prompt: str) -> str:
|
|
615 |
normalized_path = str(image_path).replace("\\", "/")
|
616 |
logger.info(f"Image saved to: {normalized_path}")
|
617 |
|
618 |
-
#return image
|
619 |
return normalized_path
|
620 |
-
|
621 |
except Exception as e:
|
622 |
logger.error(f"Error generating image: {str(e)}")
|
623 |
-
|
624 |
-
raise
|
625 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
626 |
|
627 |
|
628 |
|
|
|
498 |
# raise
|
499 |
|
500 |
|
501 |
+
## Uncomment the following code if you want to use Dalle-3 via OpenAI API
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
502 |
@mcp.tool()
|
503 |
+
def generate_image_with_dalle3(prompt: str) -> str:
|
504 |
"""
|
505 |
+
Generates an image using the Flux model via Nebius API.
|
506 |
|
507 |
+
This tool creates high-quality images from text prompts (max 2000 characters) using the FLUX.1-dev
|
508 |
+
model. The generated images are 1024x1024 pixels in PNG format and saved locally.
|
509 |
|
510 |
+
Args:
|
511 |
+
prompt (str): A descriptive text prompt for the image to generate. Be specific
|
512 |
+
about details like title, characters, dialogue, style, composition, colors, and subject
|
513 |
+
for best results.
|
514 |
|
515 |
+
Returns:
|
516 |
+
str: File path to the saved image (e.g., 'generated_images/comic_image_-620626766907224227.png'),
|
517 |
+
or an error message if image generation fails.
|
518 |
|
519 |
+
Raises:
|
520 |
+
ValueError: If no image data is returned from the API.
|
521 |
+
Exception: For other API or processing errors.
|
522 |
|
523 |
+
Example:
|
524 |
+
>>> generate_image_with_flux("A cartoon server room with smoke coming out")
|
525 |
+
"generated_images/comic_image_123456789.png"
|
526 |
+
"""
|
527 |
+
import openai
|
528 |
try:
|
529 |
+
client = openai.OpenAI()
|
|
|
|
|
|
|
|
|
530 |
response = client.images.generate(
|
531 |
+
model="dall-e-3",
|
532 |
+
prompt=prompt,
|
533 |
+
n=1,
|
534 |
+
size="1024x1024",
|
535 |
+
quality="standard",
|
536 |
+
response_format="url"
|
537 |
+
|
|
|
|
|
|
|
|
|
538 |
)
|
539 |
+
image_url = response.data[0].url
|
540 |
+
logger.info(f'Generated image URL: {image_url}')
|
|
|
541 |
|
542 |
+
# Download the image
|
543 |
+
image_response = requests.get(image_url, timeout=30)
|
544 |
+
image_response.raise_for_status()
|
545 |
+
image = Image.open(BytesIO(image_response.content))
|
546 |
|
547 |
# Save to file
|
548 |
temp_dir = Path("generated_images")
|
|
|
552 |
normalized_path = str(image_path).replace("\\", "/")
|
553 |
logger.info(f"Image saved to: {normalized_path}")
|
554 |
|
555 |
+
# return image
|
556 |
return normalized_path
|
|
|
557 |
except Exception as e:
|
558 |
logger.error(f"Error generating image: {str(e)}")
|
559 |
+
raise Exception(f"Image generation failed: {str(e)}")
|
|
|
560 |
|
561 |
+
## Uncomment the following code if you want to use Flux model via Nebius API
|
562 |
+
# @mcp.tool()
|
563 |
+
# def generate_image_with_flux(prompt: str) -> str:
|
564 |
+
# """
|
565 |
+
# Generates an image using the Flux model via Nebius API.
|
566 |
+
|
567 |
+
# This tool creates high-quality images from text prompts (max 2000 characters) using the FLUX.1-dev
|
568 |
+
# model. The generated images are 1024x1024 pixels in PNG format and saved locally.
|
569 |
+
|
570 |
+
# Args:
|
571 |
+
# prompt (str): A descriptive text prompt for the image to generate. Be specific
|
572 |
+
# about details like title, characters, dialogue, style, composition, colors, and subject
|
573 |
+
# for best results.
|
574 |
+
|
575 |
+
# Returns:
|
576 |
+
# str: File path to the saved image (e.g., 'generated_images/comic_image_-620626766907224227.png'),
|
577 |
+
# or an error message if image generation fails.
|
578 |
+
|
579 |
+
# Raises:
|
580 |
+
# ValueError: If no image data is returned from the API.
|
581 |
+
# Exception: For other API or processing errors.
|
582 |
+
|
583 |
+
# Example:
|
584 |
+
# >>> generate_image_with_flux("A cartoon server room with smoke coming out")
|
585 |
+
# "generated_images/comic_image_123456789.png"
|
586 |
+
# """
|
587 |
+
# try:
|
588 |
+
# user_prompt = f'Generate a short humorous comic book style image based on this {prompt}'
|
589 |
+
# client = OpenAI(
|
590 |
+
# base_url="https://api.studio.nebius.com/v1/",
|
591 |
+
# api_key=os.environ.get("NEBIUS_API_KEY")
|
592 |
+
# )
|
593 |
+
# response = client.images.generate(
|
594 |
+
# model="black-forest-labs/flux-dev",
|
595 |
+
# response_format="b64_json",
|
596 |
+
# extra_body={
|
597 |
+
# "response_extension": "png",
|
598 |
+
# "width": 1024,
|
599 |
+
# "height": 1024,
|
600 |
+
# "num_inference_steps": 30,
|
601 |
+
# "negative_prompt": "",
|
602 |
+
# "seed": -1
|
603 |
+
# },
|
604 |
+
# prompt=user_prompt
|
605 |
+
# )
|
606 |
+
# b64_data = response.data[0].b64_json
|
607 |
+
# if not b64_data:
|
608 |
+
# raise ValueError("No base64 image data returned in response")
|
609 |
+
|
610 |
+
# # Decode base64 and convert to PIL Image
|
611 |
+
# image_data = base64.b64decode(b64_data)
|
612 |
+
# image = Image.open(io.BytesIO(image_data))
|
613 |
+
|
614 |
+
# # Save to file
|
615 |
+
# temp_dir = Path("generated_images")
|
616 |
+
# temp_dir.mkdir(exist_ok=True)
|
617 |
+
# image_path = temp_dir / f"comic_image_{hash(prompt)}.png"
|
618 |
+
# image.save(image_path)
|
619 |
+
# normalized_path = str(image_path).replace("\\", "/")
|
620 |
+
# logger.info(f"Image saved to: {normalized_path}")
|
621 |
+
|
622 |
+
# #return image
|
623 |
+
# return normalized_path
|
624 |
+
|
625 |
+
# except Exception as e:
|
626 |
+
# logger.error(f"Error generating image: {str(e)}")
|
627 |
+
# # Return a blank image or re-raise the exception depending on your needs
|
628 |
+
# raise
|
629 |
|
630 |
|
631 |
|
outage_odyssey_ui.py
CHANGED
@@ -776,6 +776,9 @@ Actions taken: Rolled back changes, investigating access controls"""
|
|
776 |
with gr.Row():
|
777 |
with gr.Column():
|
778 |
gr.Markdown("### π¨ **Generated Comic Panels**", elem_classes="panel")
|
|
|
|
|
|
|
779 |
generate_image_button = gr.Button("π Refresh Comic Panel")
|
780 |
image_gallery = gr.Gallery(
|
781 |
label="Comic Panels",
|
|
|
776 |
with gr.Row():
|
777 |
with gr.Column():
|
778 |
gr.Markdown("### π¨ **Generated Comic Panels**", elem_classes="panel")
|
779 |
+
gr.Markdown("""
|
780 |
+
> βΉοΈ **Note:** For privacy and security reasons, your input prompts are not displayed in the Gallery tab β only the generated images are shown. Please note that this space does not use persistent storage, so generated images may be lost in case of a crash or restart. Itβs recommended to download any images you wish to keep.
|
781 |
+
""")
|
782 |
generate_image_button = gr.Button("π Refresh Comic Panel")
|
783 |
image_gallery = gr.Gallery(
|
784 |
label="Comic Panels",
|
prompts.yml
CHANGED
@@ -36,7 +36,7 @@
|
|
36 |
Here are a few examples using your available tools:
|
37 |
|
38 |
Task 1: Kubernetes pods were evicted due to memory limits being exceeded.
|
39 |
-
Thought: Follow the standard workflow: analyze_incident β generate_solution_recommendation β generate_comic_story β
|
40 |
|
41 |
Code:
|
42 |
```py
|
@@ -89,7 +89,7 @@
|
|
89 |
Thought: Generate visual comic with Flux, using the story text directly, and use the returned image path in the final response.
|
90 |
Code:
|
91 |
```py
|
92 |
-
image_path =
|
93 |
if image_path.startswith("Error generating image"):
|
94 |
print(f"Error generating image: {image_path}")
|
95 |
final_answer(f"Error: {image_path}")
|
@@ -118,7 +118,7 @@
|
|
118 |
---
|
119 |
|
120 |
Task 2: API latency spiked after failed canary deployment rollback.
|
121 |
-
Thought: Follow the standard workflow: analyze_incident β generate_solution_recommendation β generate_comic_story β
|
122 |
|
123 |
Code:
|
124 |
```py
|
@@ -171,7 +171,7 @@
|
|
171 |
Thought: Generate visual comic with Flux, using the story text directly, and use the returned image path in the final response.
|
172 |
Code:
|
173 |
```py
|
174 |
-
image_path =
|
175 |
if image_path.startswith("Error generating image"):
|
176 |
print(f"Error generating image: {image_path}")
|
177 |
final_answer(f"Error: {image_path}")
|
@@ -198,7 +198,7 @@
|
|
198 |
```<end_code>
|
199 |
---
|
200 |
Task 3: Analyze this uploaded incident report: '/path/to/incident_report.pdf' describing a server crash due to unauthorized changes.
|
201 |
-
Thought: Extract details from the uploaded PDF, then follow the standard workflow: analyze_incident β generate_solution_recommendation β generate_comic_story β
|
202 |
|
203 |
Code:
|
204 |
```py
|
@@ -264,7 +264,7 @@
|
|
264 |
Thought: Generate visual comic with Flux, using the story text directly, and use the returned image path in the final response.
|
265 |
Code:
|
266 |
```py
|
267 |
-
image_path =
|
268 |
if image_path.startswith("Error generating image"):
|
269 |
print(f"Error generating image: {image_path}")
|
270 |
final_answer(f"Error: {image_path}")
|
@@ -320,8 +320,8 @@
|
|
320 |
8. You can use imports in your code, but only from the following list of modules: {{authorized_imports}}
|
321 |
9. The state persists between code executions: variables and imports persist across steps.
|
322 |
10. Don't give up! You're in charge of solving the task completely.
|
323 |
-
11. CRITICAL: When using
|
324 |
-
12. Always follow the five-step process: analyze_incident β generate_solution_recommendation β generate_comic_story β
|
325 |
|
326 |
Your primary goal is to create educational comic strips that help prevent workplace incidents by clearly showing both what went wrong and the proper safety measures.
|
327 |
|
@@ -386,7 +386,7 @@
|
|
386 |
{{answer_facts|default('No Facts available')}}
|
387 |
```
|
388 |
|
389 |
-
Remember: Your plan must follow the mandatory sequence: analyze_incident β generate_solution_recommendation β generate_comic_story β
|
390 |
|
391 |
Now begin! Write your plan below.
|
392 |
|
@@ -420,7 +420,7 @@
|
|
420 |
Find below the record of what has been tried so far to solve it. Then you will be asked to make an updated plan to solve the task.
|
421 |
If the previous tries so far have met some success, you can make an updated plan based on these actions.
|
422 |
If you are stalled, you can make a completely new plan starting from scratch.
|
423 |
-
Remember: You must follow the five-step process: analyze_incident β generate_solution_recommendation β generate_comic_story β
|
424 |
|
425 |
"update_plan_post_messages": |-
|
426 |
You're still working towards solving this task:
|
@@ -451,7 +451,7 @@
|
|
451 |
```
|
452 |
|
453 |
Now for the given task, develop a step-by-step high-level plan taking into account the above inputs and list of facts.
|
454 |
-
This plan should involve the mandatory five-step sequence: analyze_incident β generate_solution_recommendation β generate_comic_story β
|
455 |
Beware that you have {remaining_steps} steps remaining.
|
456 |
Do not skip steps, do not add any superfluous steps. Only write the high-level plan, DO NOT DETAIL INDIVIDUAL TOOL CALLS.
|
457 |
After writing the final step of the plan, write the '\n<end_plan>' tag and stop there.
|
|
|
36 |
Here are a few examples using your available tools:
|
37 |
|
38 |
Task 1: Kubernetes pods were evicted due to memory limits being exceeded.
|
39 |
+
Thought: Follow the standard workflow: analyze_incident β generate_solution_recommendation β generate_comic_story β generate_image_with_dalle3 β final_answer
|
40 |
|
41 |
Code:
|
42 |
```py
|
|
|
89 |
Thought: Generate visual comic with Flux, using the story text directly, and use the returned image path in the final response.
|
90 |
Code:
|
91 |
```py
|
92 |
+
image_path = generate_image_with_dalle3(prompt=story)
|
93 |
if image_path.startswith("Error generating image"):
|
94 |
print(f"Error generating image: {image_path}")
|
95 |
final_answer(f"Error: {image_path}")
|
|
|
118 |
---
|
119 |
|
120 |
Task 2: API latency spiked after failed canary deployment rollback.
|
121 |
+
Thought: Follow the standard workflow: analyze_incident β generate_solution_recommendation β generate_comic_story β generate_image_with_dalle3 β final_answer.
|
122 |
|
123 |
Code:
|
124 |
```py
|
|
|
171 |
Thought: Generate visual comic with Flux, using the story text directly, and use the returned image path in the final response.
|
172 |
Code:
|
173 |
```py
|
174 |
+
image_path = generate_image_with_dalle3(prompt=story)
|
175 |
if image_path.startswith("Error generating image"):
|
176 |
print(f"Error generating image: {image_path}")
|
177 |
final_answer(f"Error: {image_path}")
|
|
|
198 |
```<end_code>
|
199 |
---
|
200 |
Task 3: Analyze this uploaded incident report: '/path/to/incident_report.pdf' describing a server crash due to unauthorized changes.
|
201 |
+
Thought: Extract details from the uploaded PDF, then follow the standard workflow: analyze_incident β generate_solution_recommendation β generate_comic_story β generate_image_with_dalle3 β final_answer.
|
202 |
|
203 |
Code:
|
204 |
```py
|
|
|
264 |
Thought: Generate visual comic with Flux, using the story text directly, and use the returned image path in the final response.
|
265 |
Code:
|
266 |
```py
|
267 |
+
image_path = generate_image_with_dalle3(prompt=story)
|
268 |
if image_path.startswith("Error generating image"):
|
269 |
print(f"Error generating image: {image_path}")
|
270 |
final_answer(f"Error: {image_path}")
|
|
|
320 |
8. You can use imports in your code, but only from the following list of modules: {{authorized_imports}}
|
321 |
9. The state persists between code executions: variables and imports persist across steps.
|
322 |
10. Don't give up! You're in charge of solving the task completely.
|
323 |
+
11. CRITICAL: When using generate_image_with_dalle3, the preferred tool for image generation, pass the story exactly as returned by generate_comic_story - do not modify, summarize, or alter the story content in any way.
|
324 |
+
12. Always follow the five-step process: analyze_incident β generate_solution_recommendation β generate_comic_story β generate_image_with_dalle3 β final_answer
|
325 |
|
326 |
Your primary goal is to create educational comic strips that help prevent workplace incidents by clearly showing both what went wrong and the proper safety measures.
|
327 |
|
|
|
386 |
{{answer_facts|default('No Facts available')}}
|
387 |
```
|
388 |
|
389 |
+
Remember: Your plan must follow the mandatory sequence: analyze_incident β generate_solution_recommendation β generate_comic_story β generate_image_with_dalle3 β final_answer
|
390 |
|
391 |
Now begin! Write your plan below.
|
392 |
|
|
|
420 |
Find below the record of what has been tried so far to solve it. Then you will be asked to make an updated plan to solve the task.
|
421 |
If the previous tries so far have met some success, you can make an updated plan based on these actions.
|
422 |
If you are stalled, you can make a completely new plan starting from scratch.
|
423 |
+
Remember: You must follow the five-step process: analyze_incident β generate_solution_recommendation β generate_comic_story β generate_image_with_dalle3 β final_answer
|
424 |
|
425 |
"update_plan_post_messages": |-
|
426 |
You're still working towards solving this task:
|
|
|
451 |
```
|
452 |
|
453 |
Now for the given task, develop a step-by-step high-level plan taking into account the above inputs and list of facts.
|
454 |
+
This plan should involve the mandatory five-step sequence: analyze_incident β generate_solution_recommendation β generate_comic_story β generate_image_with_dalle3 β final_answer
|
455 |
Beware that you have {remaining_steps} steps remaining.
|
456 |
Do not skip steps, do not add any superfluous steps. Only write the high-level plan, DO NOT DETAIL INDIVIDUAL TOOL CALLS.
|
457 |
After writing the final step of the plan, write the '\n<end_plan>' tag and stop there.
|