Spaces:
Sleeping
Sleeping
import json | |
import gradio as gr | |
import os | |
import langchain_openai | |
import langchain_core | |
from langchain_openai import ChatOpenAI | |
from langchain_core.prompts import ChatPromptTemplate | |
from langchain_core.output_parsers import StrOutputParser | |
def enhance_subject(subject, details): | |
prompt = ChatPromptTemplate.from_messages([ | |
("system", "Generate a clear and concise subject, then provide additional details using descriptive language. Ensure the response is specific and avoids ambiguity or contradictions. The subject should inspire an engaging photo that tells a story. Remove any unnecessary information and don't add any punctuation at the end of the subject."), | |
("user", "The main subject is {subject} {details}.") | |
]) | |
output_parser = StrOutputParser() | |
model = ChatOpenAI(model="gpt-3.5-turbo") | |
chain = ( prompt | |
| model | |
| output_parser | |
) | |
result = chain.invoke({"subject": subject, "details": details}) | |
return result | |
def load_input_fields(filepath): | |
""" | |
Load the input fields from a JSON file. | |
Args: | |
- filepath (str): The path to the JSON file containing the input fields. | |
Returns: | |
- dict: A dictionary containing the input fields. | |
""" | |
with open(filepath, "r") as file: | |
input_fields = json.load(file) | |
return input_fields | |
def create_html_string(input_text, highlight_color = "green", container_style = "border: 2px solid black; padding: 2px; font-size: 16px;" ): | |
""" | |
Create a HTML string with specific styles applied to highlighted text within square brackets. | |
Args: | |
- input_text (str): The input text with portions to be highlighted within square brackets. | |
- optional: highlight_color (str): Color for the highlighted text (e.g., "green"). | |
- optional: container_style (str): Any css for inline styling (e.g,, "border: 2px solid black;") | |
Returns: | |
- str: A HTML string with the applied styles. | |
""" | |
# Replace the highlighted text with HTML span elements for styling | |
highlighted_text = input_text.replace("[", f'<span style="color:{highlight_color}; font-weight: bold;">[').replace("]", "]</span>") | |
# Construct the full HTML string with the provided styles | |
html_string = f'<p style="{container_style}">{highlighted_text}</p>' | |
return html_string | |
def extract_names(objects): | |
return [obj['name'] for obj in objects if 'name' in obj] | |
def clearInput(): | |
return "" | |
def format_to_markdown(objects): | |
# Skip None objects | |
formatted_list = [ | |
f"> * **{obj.get('name', 'No Name')}** - {obj.get('description', 'No Description')}" | |
for obj in objects if obj is not None and obj["name"] != "None" | |
] | |
return '\n'.join(formatted_list) | |
find_filter_by_name = lambda collection, key: next((filter for filter in collection if filter['name'] == key), None) | |
def display_info(collection, key): | |
markdown_text = format_to_markdown([find_filter_by_name(collection, key)]) | |
return gr.Markdown(markdown_text, visible=True) | |
def enhance_pipeline(isFrog, subject, details): | |
if isFrog and (subject or details): | |
result = enhance_subject(subject, details) | |
return [gr.Textbox(visible=False), gr.Button(visible=False), gr.TextArea(visible=True, value=result)] | |
elif (subject or details) and not isFrog: | |
return [gr.Textbox(visible=True), gr.Button(visible=True), gr.TextArea(visible=False)] | |
else: | |
return [gr.Textbox(visible=False), gr.Button(visible=False), gr.TextArea(visible=False)] | |
def authenticate(pwd_input, subject, details): | |
if pwd_input == os.environ.get("MAGIC_WORD"): | |
result = enhance_subject(subject, details) | |
return [gr.TextArea(visible=True, value=result), True, gr.Textbox(visible=False), gr.Button(visible=False)] | |
else: | |
raise gr.Error("You are not from our pond! Use your own LLM to add some juice to your prompt.") | |