Spaces:
Running
Running
File size: 7,108 Bytes
2dd1349 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
from .search import GoogleSearch
from .vqa import VQA
from .image_gen import Dalle3
from .llm import LLM
from .ppt_gen import SlideAgent
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor
from pptx.enum.text import MSO_AUTO_SIZE
from pptx.dml.color import RGBColor
def search_result(question: str, screenshot_path: str = "screenshot.png") -> str:
"""
Search a question on Google, and take a screenshot of the search result.
Save the screenshot to screenshot_path, and return the path.
"""
return GoogleSearch.search_result(question, screenshot_path)
def search_image(query: str, save_path: str = 'top_images') -> str:
"""
Search for an image on Google and download the result to download_path.
Return download_path.
"""
return GoogleSearch.search_image(query, save_path)
def get_answer(question: str) -> str:
"""
Calls the LLM by inputing a question,
then get the response of the LLM as the answer
"""
return LLM.get_answer(question)
def get_code(request:str, examples:str = "") -> str:
"""
Calls the LLM to generate code for a request.
request: the task that the model should conduct
examples: few-shot code examples for the request
"""
return LLM.get_answer(request, examples)
def generate_image(query: str, save_path: str = "downloaded_image.png") -> str:
"""
Generate an image based on a text query, save the image to the save_path
Return the path of the saved image.
"""
return Dalle3.generate_image(query, save_path)
def add_title(
slide, text: str, font_size: int = 44,
font_color: tuple[int, int, int] = (0, 0, 0),
background_color: tuple[int, int, int] = None,
):
"""Add a title text to the slide with custom font size and font color (RGB tuple).
Args:
slide: Slide object as in pptx library
text: str, Title text to be added
font_size: int, Font size in int (point size), e.g., 44
font_color: tuple(int,int,int), RGB color, e.g., (0, 0, 0)
background_color: Optional, tuple(int,int,int), RGB color, e.g., (255, 255, 255)
Rets:
slide: Slide object with the title added
"""
title_shape = slide.shapes.title
if title_shape is None:
# Add a new text box as the title if no placeholder is found
title_shape = slide.shapes.add_textbox(Inches(1), Inches(0.5), Inches(8), Inches(1))
title_shape.text = text
for paragraph in title_shape.text_frame.paragraphs:
paragraph.font.size = Pt(font_size)
paragraph.font.color.rgb = RGBColor(*font_color)
if background_color is not None:
title_shape.fill.solid()
title_shape.fill.fore_color.rgb = RGBColor(*background_color)
return slide
def add_text(
slide, text: str, coords: list[float],
font_size: int = 20, bold: bool = False,
color: tuple[int, int, int] = (0, 0, 0),
background_color: tuple[int, int, int] = None,
auto_size: bool = True,
):
"""Add a text box at a specified location with custom text and color settings.
Args:
slide: Slide object as in pptx library
text: str, Text to be added
coords: list(float), [left, top, width, height] in inches
font_size: int, Font size in int (point size), e.g., 20
bold: bool, True if bold-type the text, False otherwise
color: tuple(int,int,int), RGB color, e.g., (0, 0, 0)
background_color: Optional, tuple(int,int,int), RGB color, e.g., (255, 255, 255)
auto_size: bool, True if auto-size the text box, False otherwise
Rets:
slide: Slide object with the text box added
"""
# Create the text box shape
left, top, width, height = coords
text_box = slide.shapes.add_textbox(Inches(left), Inches(top), Inches(width), Inches(height))
# Set background color if provided
if background_color:
text_box.fill.solid()
text_box.fill.fore_color.rgb = RGBColor(*background_color)
else:
text_box.fill.background() # No fill if no color is specified
# Handle line breaks and adjust height
lines = text.split("\n")
adjusted_height = height * len(lines) # Adjust height based on the number of lines
text_box.height = Inches(adjusted_height)
# Set text and format it
text_frame = text_box.text_frame
text_frame.word_wrap = True
if auto_size:
text_frame.auto_size = MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT # Automatically fit the text box to the text
p = text_frame.add_paragraph()
p.text = text
p.font.size = Pt(font_size)
p.font.bold = bold
p.font.color.rgb = RGBColor(*color)
return slide
def add_bullet_points(
slide, bullet_points: list[str], coords: list[float],
font_size: int = 18, color: tuple[int, int, int] = (0, 0, 0),
background_color: tuple[int, int, int] = None,
):
"""Add a text box with bullet points.
Args:
slide: Slide object as in pptx library
bullet_points: list(str), List of texts to be added as bullet points
coords: list(float), [left, top, width, height] in inches
font_size: int, Font size in int (point size), e.g., 18
color: tuple(int,int,int), RGB color, e.g., (0, 0, 0)
background_color: Optional, tuple(int,int,int), RGB color, e.g., (255, 255, 255)
Rets:
slide: Slide object with the bullet points added
"""
left, top, width, height = coords
text_box = slide.shapes.add_textbox(Inches(left), Inches(top), Inches(width), Inches(height))
# Set background color if provided
if background_color:
text_box.fill.solid()
text_box.fill.fore_color.rgb = RGBColor(*background_color)
else:
text_box.fill.background() # No fill if no color is specified
text_frame = text_box.text_frame
text_frame.word_wrap = True
text_frame.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE
for point in bullet_points:
p = text_frame.add_paragraph()
p.text = point
p.font.size = Pt(font_size)
p.font.color.rgb = RGBColor(*color)
# p.level = bullet_points.index(point)
return slide
def add_image(slide, image_path: str, coords: list[float]):
"""Add an image in the provided path to the specified coords and sizes.
Args:
slide: Slide object as in pptx library
image_path: str, Path to the image file
coords: list(float), [left, top, width, height] in inches
Rets:
slide: Slide object with the image added
"""
left, top, width, height = coords
slide.shapes.add_picture(image_path, Inches(left), Inches(top), Inches(width), Inches(height))
return slide
def set_background_color(slide, color: tuple[int, int, int]):
"""Set background color for the current slide.
Args:
slide: Slide object as in pptx library
color: tuple(int, int, int), RGB color, e.g., (255, 255, 255)
Returns:
modified slide object
"""
fill = slide.background.fill
fill.solid()
fill.fore_color.rgb = RGBColor(*color) # Convert tuple to RGBColor
return slide |