|
|
|
|
|
import os |
|
import re |
|
import tempfile |
|
import gc |
|
from collections.abc import Iterator |
|
from threading import Thread |
|
import json |
|
import requests |
|
import cv2 |
|
import base64 |
|
import logging |
|
import time |
|
from urllib.parse import quote |
|
|
|
import gradio as gr |
|
import spaces |
|
import torch |
|
from loguru import logger |
|
from PIL import Image |
|
from transformers import AutoProcessor, Gemma3ForConditionalGeneration, TextIteratorStreamer |
|
|
|
|
|
import pandas as pd |
|
import PyPDF2 |
|
|
|
|
|
|
|
|
|
from gradio_client import Client |
|
|
|
API_URL = "http://211.233.58.201:7896" |
|
|
|
logging.basicConfig( |
|
level=logging.DEBUG, |
|
format='%(asctime)s - %(levelname)s - %(message)s' |
|
) |
|
|
|
def test_api_connection() -> str: |
|
"""API ์๋ฒ ์ฐ๊ฒฐ ํ
์คํธ""" |
|
try: |
|
client = Client(API_URL) |
|
return "API ์ฐ๊ฒฐ ์ฑ๊ณต: ์ ์ ์๋ ์ค" |
|
except Exception as e: |
|
logging.error(f"API connection test failed: {e}") |
|
return f"API ์ฐ๊ฒฐ ์คํจ: {e}" |
|
|
|
def generate_image(prompt: str, width: float, height: float, guidance: float, inference_steps: float, seed: float): |
|
""" |
|
์ด๋ฏธ์ง ์์ฑ ํจ์. |
|
์ฌ๊ธฐ์๋ ์๋ฒ๊ฐ ์ต์ข
์ด๋ฏธ์ง๋ฅผ Base64(๋๋ data:image/...) ํํ๋ก ์ง์ ๋ฐํํ๋ค๊ณ ๊ฐ์ ํฉ๋๋ค. |
|
/tmp/... ๊ฒฝ๋ก๋ ์ถ๊ฐ ๋ค์ด๋ก๋๋ฅผ ์๋ํ์ง ์์ต๋๋ค. |
|
""" |
|
if not prompt: |
|
return None, "Error: Prompt is required" |
|
try: |
|
logging.info(f"Calling image generation API with prompt: {prompt}") |
|
|
|
client = Client(API_URL) |
|
result = client.predict( |
|
prompt=prompt, |
|
width=int(width), |
|
height=int(height), |
|
guidance=float(guidance), |
|
inference_steps=int(inference_steps), |
|
seed=int(seed), |
|
do_img2img=False, |
|
init_image=None, |
|
image2image_strength=0.8, |
|
resize_img=True, |
|
api_name="/generate_image" |
|
) |
|
|
|
logging.info( |
|
f"Image generation result: {type(result)}, " |
|
f"length: {len(result) if isinstance(result, (list, tuple)) else 'unknown'}" |
|
) |
|
|
|
|
|
if isinstance(result, (list, tuple)) and len(result) > 0: |
|
image_data = result[0] |
|
seed_info = result[1] if len(result) > 1 else "Unknown seed" |
|
return image_data, seed_info |
|
else: |
|
|
|
return result, "Unknown seed" |
|
|
|
except Exception as e: |
|
logging.error(f"Image generation failed: {str(e)}") |
|
return None, f"Error: {str(e)}" |
|
|
|
|
|
def fix_base64_padding(data): |
|
"""Base64 ๋ฌธ์์ด์ ํจ๋ฉ์ ์์ ํฉ๋๋ค.""" |
|
if isinstance(data, bytes): |
|
data = data.decode('utf-8') |
|
|
|
if "base64," in data: |
|
data = data.split("base64,", 1)[1] |
|
|
|
missing_padding = len(data) % 4 |
|
if missing_padding: |
|
data += '=' * (4 - missing_padding) |
|
|
|
return data |
|
|
|
|
|
|
|
|
|
def clear_cuda_cache(): |
|
"""CUDA ์บ์๋ฅผ ๋ช
์์ ์ผ๋ก ๋น์๋๋ค.""" |
|
if torch.cuda.is_available(): |
|
torch.cuda.empty_cache() |
|
gc.collect() |
|
|
|
|
|
|
|
|
|
SERPHOUSE_API_KEY = os.getenv("SERPHOUSE_API_KEY", "") |
|
|
|
def extract_keywords(text: str, top_k: int = 5) -> str: |
|
"""๋จ์ ํค์๋ ์ถ์ถ: ํ๊ธ, ์์ด, ์ซ์, ๊ณต๋ฐฑ๋ง ๋จ๊น""" |
|
text = re.sub(r"[^a-zA-Z0-9๊ฐ-ํฃ\s]", "", text) |
|
tokens = text.split() |
|
return " ".join(tokens[:top_k]) |
|
|
|
def do_web_search(query: str) -> str: |
|
""" |
|
SerpHouse LIVE API ํธ์ถํ์ฌ ๊ฒ์ ๊ฒฐ๊ณผ ๋งํฌ๋ค์ด ๋ฐํ |
|
(ํ์ํ๋ค๋ฉด ์์ or ์ญ์ ๊ฐ๋ฅ) |
|
""" |
|
try: |
|
url = "https://api.serphouse.com/serp/live" |
|
params = { |
|
"q": query, |
|
"domain": "google.com", |
|
"serp_type": "web", |
|
"device": "desktop", |
|
"lang": "en", |
|
"num": "20" |
|
} |
|
headers = {"Authorization": f"Bearer {SERPHOUSE_API_KEY}"} |
|
logger.info(f"SerpHouse API ํธ์ถ ์ค... ๊ฒ์์ด: {query}") |
|
response = requests.get(url, headers=headers, params=params, timeout=60) |
|
response.raise_for_status() |
|
data = response.json() |
|
results = data.get("results", {}) |
|
organic = None |
|
if isinstance(results, dict) and "organic" in results: |
|
organic = results["organic"] |
|
elif isinstance(results, dict) and "results" in results: |
|
if isinstance(results["results"], dict) and "organic" in results["results"]: |
|
organic = results["results"]["organic"] |
|
elif "organic" in data: |
|
organic = data["organic"] |
|
if not organic: |
|
logger.warning("์๋ต์์ organic ๊ฒฐ๊ณผ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.") |
|
return "No web search results found or unexpected API response structure." |
|
max_results = min(20, len(organic)) |
|
limited_organic = organic[:max_results] |
|
summary_lines = [] |
|
for idx, item in enumerate(limited_organic, start=1): |
|
title = item.get("title", "No title") |
|
link = item.get("link", "#") |
|
snippet = item.get("snippet", "No description") |
|
displayed_link = item.get("displayed_link", link) |
|
summary_lines.append( |
|
f"### Result {idx}: {title}\n\n" |
|
f"{snippet}\n\n" |
|
f"**์ถ์ฒ**: [{displayed_link}]({link})\n\n" |
|
f"---\n" |
|
) |
|
instructions = """ |
|
# ์น ๊ฒ์ ๊ฒฐ๊ณผ |
|
์๋๋ ๊ฒ์ ๊ฒฐ๊ณผ์
๋๋ค. ์ง๋ฌธ์ ๋ต๋ณํ ๋ ์ด ์ ๋ณด๋ฅผ ํ์ฉํ์ธ์: |
|
1. ์ฌ๋ฌ ์ถ์ฒ ๋ด์ฉ์ ์ข
ํฉํ์ฌ ๋ต๋ณ. |
|
2. ์ถ์ฒ ์ธ์ฉ ์ "[์ถ์ฒ ์ ๋ชฉ](๋งํฌ)" ๋งํฌ๋ค์ด ํ์ ์ฌ์ฉ. |
|
3. ๋ต๋ณ ๋ง์ง๋ง์ '์ฐธ๊ณ ์๋ฃ:' ์น์
์ ์ฌ์ฉํ ์ฃผ์ ์ถ์ฒ๋ฅผ ๋์ด. |
|
""" |
|
return instructions + "\n".join(summary_lines) |
|
except Exception as e: |
|
logger.error(f"Web search failed: {e}") |
|
return f"Web search failed: {str(e)}" |
|
|
|
|
|
|
|
|
|
MAX_CONTENT_CHARS = 2000 |
|
MAX_INPUT_LENGTH = 2096 |
|
|
|
model_id = os.getenv("MODEL_ID", "VIDraft/Gemma-3-R1984-4B") |
|
processor = AutoProcessor.from_pretrained(model_id, padding_side="left") |
|
model = Gemma3ForConditionalGeneration.from_pretrained( |
|
model_id, |
|
device_map="auto", |
|
torch_dtype=torch.bfloat16, |
|
attn_implementation="eager" |
|
) |
|
|
|
MAX_NUM_IMAGES = int(os.getenv("MAX_NUM_IMAGES", "5")) |
|
|
|
|
|
|
|
|
|
def analyze_csv_file(path: str) -> str: |
|
try: |
|
df = pd.read_csv(path) |
|
if df.shape[0] > 50 or df.shape[1] > 10: |
|
df = df.iloc[:50, :10] |
|
df_str = df.to_string() |
|
if len(df_str) > MAX_CONTENT_CHARS: |
|
df_str = df_str[:MAX_CONTENT_CHARS] + "\n...(truncated)..." |
|
return f"**[CSV File: {os.path.basename(path)}]**\n\n{df_str}" |
|
except Exception as e: |
|
return f"Failed to read CSV ({os.path.basename(path)}): {str(e)}" |
|
|
|
def analyze_txt_file(path: str) -> str: |
|
try: |
|
with open(path, "r", encoding="utf-8") as f: |
|
text = f.read() |
|
if len(text) > MAX_CONTENT_CHARS: |
|
text = text[:MAX_CONTENT_CHARS] + "\n...(truncated)..." |
|
return f"**[TXT File: {os.path.basename(path)}]**\n\n{text}" |
|
except Exception as e: |
|
return f"Failed to read TXT ({os.path.basename(path)}): {str(e)}" |
|
|
|
def pdf_to_markdown(pdf_path: str) -> str: |
|
text_chunks = [] |
|
try: |
|
with open(pdf_path, "rb") as f: |
|
reader = PyPDF2.PdfReader(f) |
|
max_pages = min(5, len(reader.pages)) |
|
for page_num in range(max_pages): |
|
page_text = reader.pages[page_num].extract_text() or "" |
|
page_text = page_text.strip() |
|
if page_text: |
|
if len(page_text) > MAX_CONTENT_CHARS // max_pages: |
|
page_text = page_text[:MAX_CONTENT_CHARS // max_pages] + "...(truncated)" |
|
text_chunks.append(f"## Page {page_num+1}\n\n{page_text}\n") |
|
if len(reader.pages) > max_pages: |
|
text_chunks.append(f"\n...(Showing {max_pages} of {len(reader.pages)} pages)...") |
|
except Exception as e: |
|
return f"Failed to read PDF ({os.path.basename(pdf_path)}): {str(e)}" |
|
full_text = "\n".join(text_chunks) |
|
if len(full_text) > MAX_CONTENT_CHARS: |
|
full_text = full_text[:MAX_CONTENT_CHARS] + "\n...(truncated)..." |
|
return f"**[PDF File: {os.path.basename(pdf_path)}]**\n\n{full_text}" |
|
|
|
|
|
|
|
|
|
def count_files_in_new_message(paths: list[str]) -> tuple[int, int]: |
|
image_count = 0 |
|
video_count = 0 |
|
for path in paths: |
|
if path.endswith(".mp4"): |
|
video_count += 1 |
|
elif re.search(r"\.(png|jpg|jpeg|gif|webp)$", path, re.IGNORECASE): |
|
image_count += 1 |
|
return image_count, video_count |
|
|
|
def count_files_in_history(history: list[dict]) -> tuple[int, int]: |
|
image_count = 0 |
|
video_count = 0 |
|
for item in history: |
|
if item["role"] != "user" or isinstance(item["content"], str): |
|
continue |
|
if isinstance(item["content"], list) and len(item["content"]) > 0: |
|
file_path = item["content"][0] |
|
if isinstance(file_path, str): |
|
if file_path.endswith(".mp4"): |
|
video_count += 1 |
|
elif re.search(r"\.(png|jpg|jpeg|gif|webp)$", file_path, re.IGNORECASE): |
|
image_count += 1 |
|
return image_count, video_count |
|
|
|
def validate_media_constraints(message: dict, history: list[dict]) -> bool: |
|
"""์ด๋ฏธ์ง/๋น๋์ค ์
๋ก๋ ์ ํ ๊ฒ์ฌ.""" |
|
media_files = [f for f in message["files"] |
|
if re.search(r"\.(png|jpg|jpeg|gif|webp)$", f, re.IGNORECASE) or f.endswith(".mp4")] |
|
new_image_count, new_video_count = count_files_in_new_message(media_files) |
|
history_image_count, history_video_count = count_files_in_history(history) |
|
|
|
image_count = history_image_count + new_image_count |
|
video_count = history_video_count + new_video_count |
|
|
|
if video_count > 1: |
|
gr.Warning("Only one video is supported.") |
|
return False |
|
if video_count == 1: |
|
if image_count > 0: |
|
gr.Warning("Mixing images and videos is not allowed.") |
|
return False |
|
if "<image>" in message["text"]: |
|
gr.Warning("Using <image> tags with video files is not supported.") |
|
return False |
|
if video_count == 0 and image_count > MAX_NUM_IMAGES: |
|
gr.Warning(f"You can upload up to {MAX_NUM_IMAGES} images.") |
|
return False |
|
if "<image>" in message["text"]: |
|
image_files = [f for f in message["files"] |
|
if re.search(r"\.(png|jpg|jpeg|gif|webp)$", f, re.IGNORECASE)] |
|
image_tag_count = message["text"].count("<image>") |
|
if image_tag_count != len(image_files): |
|
gr.Warning("The number of <image> tags in the text does not match the number of image files.") |
|
return False |
|
return True |
|
|
|
|
|
|
|
|
|
def downsample_video(video_path: str) -> list[tuple[Image.Image, float]]: |
|
vidcap = cv2.VideoCapture(video_path) |
|
fps = vidcap.get(cv2.CAP_PROP_FPS) |
|
total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT)) |
|
frame_interval = max(int(fps), int(total_frames / 10)) |
|
frames = [] |
|
for i in range(0, total_frames, frame_interval): |
|
vidcap.set(cv2.CAP_PROP_POS_FRAMES, i) |
|
success, image = vidcap.read() |
|
if success: |
|
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
|
image = cv2.resize(image, (0, 0), fx=0.5, fy=0.5) |
|
pil_image = Image.fromarray(image) |
|
timestamp = round(i / fps, 2) |
|
frames.append((pil_image, timestamp)) |
|
if len(frames) >= 5: |
|
break |
|
vidcap.release() |
|
return frames |
|
|
|
def process_video(video_path: str) -> tuple[list[dict], list[str]]: |
|
content = [] |
|
temp_files = [] |
|
frames = downsample_video(video_path) |
|
for pil_image, timestamp in frames: |
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file: |
|
pil_image.save(temp_file.name) |
|
temp_files.append(temp_file.name) |
|
content.append({"type": "text", "text": f"Frame {timestamp}:"}) |
|
content.append({"type": "image", "url": temp_file.name}) |
|
return content, temp_files |
|
|
|
|
|
|
|
|
|
def process_interleaved_images(message: dict) -> list[dict]: |
|
parts = re.split(r"(<image>)", message["text"]) |
|
content = [] |
|
image_files = [f for f in message["files"] |
|
if re.search(r"\.(png|jpg|jpeg|gif|webp)$", f, re.IGNORECASE)] |
|
image_index = 0 |
|
for part in parts: |
|
if part == "<image>" and image_index < len(image_files): |
|
content.append({"type": "image", "url": image_files[image_index]}) |
|
image_index += 1 |
|
elif part.strip(): |
|
content.append({"type": "text", "text": part.strip()}) |
|
else: |
|
if isinstance(part, str) and part != "<image>": |
|
content.append({"type": "text", "text": part}) |
|
return content |
|
|
|
|
|
|
|
|
|
def is_image_file(file_path: str) -> bool: |
|
return bool(re.search(r"\.(png|jpg|jpeg|gif|webp)$", file_path, re.IGNORECASE)) |
|
|
|
def is_video_file(file_path: str) -> bool: |
|
return file_path.endswith(".mp4") |
|
|
|
def is_document_file(file_path: str) -> bool: |
|
return file_path.lower().endswith(".pdf") or file_path.lower().endswith(".csv") or file_path.lower().endswith(".txt") |
|
|
|
def process_new_user_message(message: dict) -> tuple[list[dict], list[str]]: |
|
"""์ฌ์ฉ์๊ฐ ์๋ก ์
๋ ฅํ ๋ฉ์์ง + ์
๋ก๋ ํ์ผ๋ค์ ํ๋์ content(list)๋ก ๋ณํ.""" |
|
temp_files = [] |
|
if not message["files"]: |
|
return [{"type": "text", "text": message["text"]}], temp_files |
|
|
|
video_files = [f for f in message["files"] if is_video_file(f)] |
|
image_files = [f for f in message["files"] if is_image_file(f)] |
|
csv_files = [f for f in message["files"] if f.lower().endswith(".csv")] |
|
txt_files = [f for f in message["files"] if f.lower().endswith(".txt")] |
|
pdf_files = [f for f in message["files"] if f.lower().endswith(".pdf")] |
|
|
|
content_list = [{"type": "text", "text": message["text"]}] |
|
|
|
|
|
for csv_path in csv_files: |
|
content_list.append({"type": "text", "text": analyze_csv_file(csv_path)}) |
|
for txt_path in txt_files: |
|
content_list.append({"type": "text", "text": analyze_txt_file(txt_path)}) |
|
for pdf_path in pdf_files: |
|
content_list.append({"type": "text", "text": pdf_to_markdown(pdf_path)}) |
|
|
|
|
|
if video_files: |
|
video_content, video_temp_files = process_video(video_files[0]) |
|
content_list += video_content |
|
temp_files.extend(video_temp_files) |
|
return content_list, temp_files |
|
|
|
|
|
if "<image>" in message["text"] and image_files: |
|
interleaved_content = process_interleaved_images({"text": message["text"], "files": image_files}) |
|
if content_list and content_list[0]["type"] == "text": |
|
content_list = content_list[1:] |
|
return interleaved_content + content_list, temp_files |
|
else: |
|
for img_path in image_files: |
|
content_list.append({"type": "image", "url": img_path}) |
|
|
|
return content_list, temp_files |
|
|
|
|
|
|
|
|
|
def process_history(history: list[dict]) -> list[dict]: |
|
""" |
|
๊ธฐ์กด ๋ํ ๊ธฐ๋ก์ LLM์ ๋ง๊ฒ ๋ณํ. |
|
- user -> {"role":"user","content":[{type,text},...]} |
|
- assistant -> {"role":"assistant","content":[{type:"text",text},...]} |
|
""" |
|
messages = [] |
|
current_user_content = [] |
|
for item in history: |
|
if item["role"] == "assistant": |
|
|
|
if current_user_content: |
|
messages.append({"role": "user", "content": current_user_content}) |
|
current_user_content = [] |
|
|
|
messages.append({"role": "assistant", "content": [{"type": "text", "text": item["content"]}]}) |
|
else: |
|
content = item["content"] |
|
if isinstance(content, str): |
|
current_user_content.append({"type": "text", "text": content}) |
|
elif isinstance(content, list) and len(content) > 0: |
|
file_path = content[0] |
|
if is_image_file(file_path): |
|
current_user_content.append({"type": "image", "url": file_path}) |
|
else: |
|
current_user_content.append({"type": "text", "text": f"[File: {os.path.basename(file_path)}]"}) |
|
if current_user_content: |
|
messages.append({"role": "user", "content": current_user_content}) |
|
return messages |
|
|
|
|
|
|
|
|
|
def _model_gen_with_oom_catch(**kwargs): |
|
try: |
|
model.generate(**kwargs) |
|
except torch.cuda.OutOfMemoryError: |
|
raise RuntimeError("[OutOfMemoryError] GPU ๋ฉ๋ชจ๋ฆฌ๊ฐ ๋ถ์กฑํฉ๋๋ค.") |
|
finally: |
|
clear_cuda_cache() |
|
|
|
|
|
|
|
|
|
@spaces.GPU(duration=120) |
|
def run( |
|
message: dict, |
|
history: list[dict], |
|
system_prompt: str = "", |
|
max_new_tokens: int = 512, |
|
use_web_search: bool = False, |
|
web_search_query: str = "", |
|
age_group: str = "20๋", |
|
mbti_personality: str = "INTP", |
|
sexual_openness: int = 2, |
|
image_gen: bool = False |
|
) -> Iterator[str]: |
|
""" |
|
LLM ์ถ๋ก ํจ์. |
|
- ์ด๋ฏธ์ง ์์ฑ ์, ์๋ฒ๊ฐ Base64(๋๋ data:image/... ํํ)๋ฅผ ์ง์ ๋ฐํํ๋ค๊ณ ๊ฐ์ . |
|
- /tmp/... ํ์ผ์ ๋ํ ์ฌ๋ค์ด๋ก๋๋ฅผ ์๋ํ์ง ์์ (403 Forbidden ๋ฌธ์ ํํผ). |
|
""" |
|
if not validate_media_constraints(message, history): |
|
yield "" |
|
return |
|
|
|
temp_files = [] |
|
try: |
|
|
|
persona = ( |
|
f"{system_prompt.strip()}\n\n" |
|
f"Gender: Female\n" |
|
f"Age Group: {age_group}\n" |
|
f"MBTI Persona: {mbti_personality}\n" |
|
f"Sexual Openness (1~5): {sexual_openness}\n" |
|
) |
|
combined_system_msg = f"[System Prompt]\n{persona.strip()}\n\n" |
|
|
|
|
|
if use_web_search: |
|
user_text = message["text"] |
|
ws_query = extract_keywords(user_text) |
|
if ws_query.strip(): |
|
logger.info(f"[Auto WebSearch Keyword] {ws_query!r}") |
|
ws_result = do_web_search(ws_query) |
|
combined_system_msg += f"[Search top-20 Full Items]\n{ws_result}\n\n" |
|
combined_system_msg += ( |
|
"[์ฐธ๊ณ : ์ ๊ฒ์๊ฒฐ๊ณผ link๋ฅผ ์ถ์ฒ๋ก ์ธ์ฉํ์ฌ ๋ต๋ณ]\n" |
|
"[์ค์ ์ง์์ฌํญ]\n" |
|
"1. ๊ฒ์ ๊ฒฐ๊ณผ์์ ์ฐพ์ ์ ๋ณด์ ์ถ์ฒ๋ฅผ ๋ฐ๋์ ์ธ์ฉ.\n" |
|
"2. '[์ถ์ฒ ์ ๋ชฉ](๋งํฌ)' ํ์์ผ๋ก ๋งํฌ.\n" |
|
"3. ๋ต๋ณ ๋ง์ง๋ง์ '์ฐธ๊ณ ์๋ฃ:' ์น์
.\n" |
|
) |
|
else: |
|
combined_system_msg += "[No valid keywords found, skipping WebSearch]\n\n" |
|
|
|
|
|
messages = [] |
|
if combined_system_msg.strip(): |
|
messages.append({"role": "system", "content": [{"type": "text", "text": combined_system_msg.strip()}]}) |
|
messages.extend(process_history(history)) |
|
|
|
user_content, user_temp_files = process_new_user_message(message) |
|
temp_files.extend(user_temp_files) |
|
|
|
for item in user_content: |
|
if item["type"] == "text" and len(item["text"]) > MAX_CONTENT_CHARS: |
|
item["text"] = item["text"][:MAX_CONTENT_CHARS] + "\n...(truncated)..." |
|
|
|
messages.append({"role": "user", "content": user_content}) |
|
|
|
|
|
inputs = processor.apply_chat_template( |
|
messages, |
|
add_generation_prompt=True, |
|
tokenize=True, |
|
return_dict=True, |
|
return_tensors="pt", |
|
).to(device=model.device, dtype=torch.bfloat16) |
|
if inputs.input_ids.shape[1] > MAX_INPUT_LENGTH: |
|
inputs.input_ids = inputs.input_ids[:, -MAX_INPUT_LENGTH:] |
|
if 'attention_mask' in inputs: |
|
inputs.attention_mask = inputs.attention_mask[:, -MAX_INPUT_LENGTH:] |
|
|
|
streamer = TextIteratorStreamer(processor, timeout=30.0, skip_prompt=True, skip_special_tokens=True) |
|
gen_kwargs = dict(inputs, streamer=streamer, max_new_tokens=max_new_tokens) |
|
|
|
t = Thread(target=_model_gen_with_oom_catch, kwargs=gen_kwargs) |
|
t.start() |
|
|
|
|
|
output_so_far = "" |
|
for new_text in streamer: |
|
output_so_far += new_text |
|
yield output_so_far |
|
|
|
|
|
if image_gen: |
|
last_user_text = message["text"].strip() |
|
if not last_user_text: |
|
yield output_so_far + "\n\n(์ด๋ฏธ์ง ์์ฑ ์คํจ: Empty user prompt)" |
|
else: |
|
try: |
|
width, height = 512, 512 |
|
guidance, steps, seed = 7.5, 30, 42 |
|
|
|
logger.info(f"Generating image with prompt: {last_user_text}") |
|
|
|
|
|
image_result, seed_info = generate_image( |
|
prompt=last_user_text, |
|
width=width, |
|
height=height, |
|
guidance=guidance, |
|
inference_steps=steps, |
|
seed=seed |
|
) |
|
|
|
logger.info(f"Received image data type: {type(image_result)}") |
|
|
|
|
|
if image_result: |
|
if isinstance(image_result, str): |
|
|
|
if image_result.startswith("data:image/"): |
|
final_md = f"\n\n**[์์ฑ๋ ์ด๋ฏธ์ง]**\n\n" |
|
yield output_so_far + final_md |
|
else: |
|
|
|
if len(image_result) > 100 and "/" not in image_result: |
|
|
|
image_data = "data:image/webp;base64," + image_result |
|
final_md = f"\n\n**[์์ฑ๋ ์ด๋ฏธ์ง]**\n\n" |
|
yield output_so_far + final_md |
|
else: |
|
|
|
yield output_so_far + "\n\n(์ด๋ฏธ์ง ์์ฑ ๊ฒฐ๊ณผ๊ฐ base64 ํ์์ด ์๋๋๋ค)" |
|
else: |
|
yield output_so_far + "\n\n(์ด๋ฏธ์ง ์์ฑ ๊ฒฐ๊ณผ๊ฐ ๋ฌธ์์ด์ด ์๋)" |
|
else: |
|
yield output_so_far + f"\n\n(์ด๋ฏธ์ง ์์ฑ ์คํจ: {seed_info})" |
|
|
|
except Exception as e: |
|
logger.error(f"Image generation error: {e}") |
|
yield output_so_far + f"\n\n(์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {e})" |
|
|
|
except Exception as e: |
|
logger.error(f"Error in run: {str(e)}") |
|
yield f"์ฃ์กํฉ๋๋ค. ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}" |
|
finally: |
|
for tmp in temp_files: |
|
try: |
|
if os.path.exists(tmp): |
|
os.unlink(tmp) |
|
logger.info(f"Deleted temp file: {tmp}") |
|
except Exception as ee: |
|
logger.warning(f"Failed to delete temp file {tmp}: {ee}") |
|
try: |
|
del inputs, streamer |
|
except Exception: |
|
pass |
|
clear_cuda_cache() |
|
|
|
|
|
|
|
|
|
examples = [ |
|
[ |
|
{ |
|
"text": "Compare the contents of the two PDF files.", |
|
"files": [ |
|
"assets/additional-examples/before.pdf", |
|
"assets/additional-examples/after.pdf", |
|
], |
|
} |
|
], |
|
[ |
|
{ |
|
"text": "Summarize and analyze the contents of the CSV file.", |
|
"files": ["assets/additional-examples/sample-csv.csv"], |
|
} |
|
], |
|
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
css = """ |
|
.gradio-container { |
|
background: rgba(255, 255, 255, 0.7); |
|
padding: 30px 40px; |
|
margin: 20px auto; |
|
width: 100% !important; |
|
max-width: none !important; |
|
} |
|
""" |
|
title_html = """ |
|
<h1 align="center" style="margin-bottom: 0.2em; font-size: 1.6em;"> ๐ HeartSync : Love Dating AI ๐ </h1> |
|
<p align="center" style="font-size:1.1em; color:#555;"> |
|
โ
FLUX Image Generation โ
Reasoning & Uncensored โ
Multimodal & VLM โ
Deep-Research & RAG <br> |
|
</p> |
|
""" |
|
|
|
with gr.Blocks(css=css, title="HeartSync") as demo: |
|
gr.Markdown(title_html) |
|
|
|
|
|
generated_images = gr.Gallery( |
|
label="์์ฑ๋ ์ด๋ฏธ์ง", |
|
show_label=True, |
|
visible=False, |
|
elem_id="generated_images", |
|
columns=2, |
|
height="auto", |
|
object_fit="contain" |
|
) |
|
|
|
with gr.Row(): |
|
web_search_checkbox = gr.Checkbox(label="Deep Research", value=False) |
|
image_gen_checkbox = gr.Checkbox(label="Image Gen", value=False) |
|
|
|
base_system_prompt_box = gr.Textbox( |
|
lines=3, |
|
value="You are a deep thinking AI...\nํ๋ฅด์๋: ๋น์ ์ ๋ฌ์ฝคํ๊ณ ...", |
|
label="๊ธฐ๋ณธ ์์คํ
ํ๋กฌํํธ", |
|
visible=False |
|
) |
|
with gr.Row(): |
|
age_group_dropdown = gr.Dropdown( |
|
label="์ฐ๋ น๋ ์ ํ (๊ธฐ๋ณธ 20๋)", |
|
choices=["10๋", "20๋", "30~40๋", "50~60๋", "70๋ ์ด์"], |
|
value="20๋", |
|
interactive=True |
|
) |
|
mbti_choices = [ |
|
"INTJ (์ฉ์์ฃผ๋ํ ์ ๋ต๊ฐ)", |
|
"INTP (๋
ผ๋ฆฌ์ ์ธ ์ฌ์๊ฐ)", |
|
"ENTJ (๋๋ดํ ํต์์)", |
|
"ENTP (๋จ๊ฑฐ์ด ๋
ผ์๊ฐ)", |
|
"INFJ (์ ์์ ์นํธ์)", |
|
"INFP (์ด์ ์ ์ธ ์ค์ฌ์)", |
|
"ENFJ (์ ์๋ก์ด ์ฌํ์ด๋๊ฐ)", |
|
"ENFP (์ฌ๊ธฐ๋ฐ๋ํ ํ๋๊ฐ)", |
|
"ISTJ (์ฒญ๋ ด๊ฒฐ๋ฐฑํ ๋
ผ๋ฆฌ์ฃผ์์)", |
|
"ISFJ (์ฉ๊ฐํ ์ํธ์)", |
|
"ESTJ (์๊ฒฉํ ๊ด๋ฆฌ์)", |
|
"ESFJ (์ฌ๊ต์ ์ธ ์ธ๊ต๊ด)", |
|
"ISTP (๋ง๋ฅ ์ฌ์ฃผ๊พผ)", |
|
"ISFP (ํธ๊ธฐ์ฌ ๋ง์ ์์ ๊ฐ)", |
|
"ESTP (๋ชจํ์ ์ฆ๊ธฐ๋ ์ฌ์
๊ฐ)", |
|
"ESFP (์์ ๋ก์ด ์ํผ์ ์ฐ์์ธ)" |
|
] |
|
mbti_dropdown = gr.Dropdown( |
|
label="AI ํ๋ฅด์๋ MBTI (๊ธฐ๋ณธ INTP)", |
|
choices=mbti_choices, |
|
value="INTP (๋
ผ๋ฆฌ์ ์ธ ์ฌ์๊ฐ)", |
|
interactive=True |
|
) |
|
sexual_openness_slider = gr.Slider( |
|
minimum=1, maximum=5, step=1, value=2, |
|
label="์น์์ผ ๊ด์ฌ๋/๊ฐ๋ฐฉ์ฑ (1~5, ๊ธฐ๋ณธ=2)", |
|
interactive=True |
|
) |
|
max_tokens_slider = gr.Slider( |
|
label="Max New Tokens", |
|
minimum=100, maximum=8000, step=50, value=1000, |
|
visible=False |
|
) |
|
web_search_text = gr.Textbox( |
|
lines=1, |
|
label="(Unused) Web Search Query", |
|
placeholder="No direct input needed", |
|
visible=False |
|
) |
|
|
|
def modified_run( |
|
message, history, system_prompt, max_new_tokens, |
|
use_web_search, web_search_query, |
|
age_group, mbti_personality, sexual_openness, image_gen |
|
): |
|
""" |
|
run() ํจ์๋ฅผ ํธ์ถํ์ฌ ํ
์คํธ ์คํธ๋ฆผ์ ๋ฐ๊ณ , |
|
ํ์ ์ ์ถ๊ฐ ์ฒ๋ฆฌ ํ ๊ฒฐ๊ณผ ๋ฐํ (๊ฐค๋ฌ๋ฆฌ ์
๋ฐ์ดํธ ๋ฑ). |
|
""" |
|
output_so_far = "" |
|
gallery_update = gr.Gallery(visible=False, value=[]) |
|
yield output_so_far, gallery_update |
|
|
|
text_generator = run( |
|
message, history, |
|
system_prompt, max_new_tokens, |
|
use_web_search, web_search_query, |
|
age_group, mbti_personality, |
|
sexual_openness, image_gen |
|
) |
|
|
|
for text_chunk in text_generator: |
|
output_so_far = text_chunk |
|
yield output_so_far, gallery_update |
|
|
|
|
|
|
|
|
|
|
|
chat = gr.ChatInterface( |
|
fn=modified_run, |
|
type="messages", |
|
chatbot=gr.Chatbot(type="messages", scale=1, allow_tags=["image"]), |
|
textbox=gr.MultimodalTextbox( |
|
file_types=[".webp", ".png", ".jpg", ".jpeg", ".gif", ".mp4", ".csv", ".txt", ".pdf"], |
|
file_count="multiple", |
|
autofocus=True |
|
), |
|
multimodal=True, |
|
additional_inputs=[ |
|
base_system_prompt_box, |
|
max_tokens_slider, |
|
web_search_checkbox, |
|
web_search_text, |
|
age_group_dropdown, |
|
mbti_dropdown, |
|
sexual_openness_slider, |
|
image_gen_checkbox, |
|
], |
|
additional_outputs=[generated_images], |
|
stop_btn=False, |
|
title='<a href="https://discord.gg/openfreeai" target="_blank">https://discord.gg/openfreeai</a>', |
|
examples=examples, |
|
run_examples_on_click=False, |
|
cache_examples=False, |
|
css_paths=None, |
|
delete_cache=(1800, 1800), |
|
) |
|
|
|
with gr.Row(elem_id="examples_row"): |
|
with gr.Column(scale=12, elem_id="examples_container"): |
|
gr.Markdown("### Example Inputs (click to load)") |
|
|
|
if __name__ == "__main__": |
|
demo.launch(share=True) |
|
|