Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,6 +33,128 @@ from diffusers import FluxPipeline
|
|
| 33 |
import torch
|
| 34 |
from os import path # ์ด ์ค์ ์ถ๊ฐ
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# ์บ์ ๊ฒฝ๋ก ์ค์
|
| 37 |
cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
|
| 38 |
os.environ["TRANSFORMERS_CACHE"] = cache_path
|
|
@@ -691,6 +813,42 @@ def create_main_interface():
|
|
| 691 |
.custom-button:hover {
|
| 692 |
background: #0056b3;
|
| 693 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 694 |
""", theme=theme)
|
| 695 |
|
| 696 |
|
|
@@ -860,6 +1018,9 @@ def create_main_interface():
|
|
| 860 |
# ์ข์ธก ํจ๋
|
| 861 |
with antd.Col(span=24, md=8):
|
| 862 |
with antd.Flex(vertical=True, gap="middle", wrap=True):
|
|
|
|
|
|
|
|
|
|
| 863 |
# macOS ์คํ์ผ ์๋์ฐ ํค๋
|
| 864 |
header = gr.HTML("""
|
| 865 |
<div class="window-frame">
|
|
@@ -885,16 +1046,37 @@ Use the "Generate" button for basic creation, "Enhance" button for prompt improv
|
|
| 885 |
</div>
|
| 886 |
""".format(get_image_base64('mouse.gif')))
|
| 887 |
|
| 888 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 889 |
input = antd.InputTextarea(
|
| 890 |
size="large",
|
| 891 |
allow_clear=True,
|
| 892 |
placeholder=get_random_placeholder(),
|
| 893 |
-
elem_classes="custom-textarea"
|
| 894 |
-
|
| 895 |
)
|
| 896 |
-
|
| 897 |
-
|
| 898 |
with antd.Flex(gap="small", justify="flex-start"):
|
| 899 |
btn = antd.Button(
|
| 900 |
"Generate",
|
|
@@ -915,11 +1097,63 @@ Use the "Generate" button for basic creation, "Enhance" button for prompt improv
|
|
| 915 |
elem_classes="share-btn"
|
| 916 |
)
|
| 917 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 918 |
|
| 919 |
-
|
| 920 |
-
|
| 921 |
-
|
| 922 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 923 |
|
| 924 |
# ์ฐ์ธก ํจ๋
|
| 925 |
with antd.Col(span=24, md=16):
|
|
|
|
| 33 |
import torch
|
| 34 |
from os import path # ์ด ์ค์ ์ถ๊ฐ
|
| 35 |
|
| 36 |
+
# ํ์ผ ์
๋ก๋ ๊ด๋ จ ์๋ก์ด imports ์ถ๊ฐ
|
| 37 |
+
from PIL import Image
|
| 38 |
+
import pytesseract
|
| 39 |
+
import fitz # PyMuPDF for PDF processing
|
| 40 |
+
import docx
|
| 41 |
+
import io
|
| 42 |
+
import mimetypes
|
| 43 |
+
|
| 44 |
+
# ํ์ผ ์ฒ๋ฆฌ๋ฅผ ์ํ ์๋ก์ด ํด๋์ค ์ ์
|
| 45 |
+
class FileProcessor:
|
| 46 |
+
ALLOWED_EXTENSIONS = {
|
| 47 |
+
'image': ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp'],
|
| 48 |
+
'text': ['.txt', '.pdf', '.docx', '.rtf'],
|
| 49 |
+
}
|
| 50 |
+
MAX_FILE_SIZE = 50 * 1024 * 1024 # 50MB
|
| 51 |
+
MAX_FILES = 5
|
| 52 |
+
|
| 53 |
+
@staticmethod
|
| 54 |
+
def is_allowed_file(filename):
|
| 55 |
+
ext = os.path.splitext(filename)[1].lower()
|
| 56 |
+
allowed = []
|
| 57 |
+
for extensions in FileProcessor.ALLOWED_EXTENSIONS.values():
|
| 58 |
+
allowed.extend(extensions)
|
| 59 |
+
return ext in allowed
|
| 60 |
+
|
| 61 |
+
@staticmethod
|
| 62 |
+
def get_file_type(filename):
|
| 63 |
+
ext = os.path.splitext(filename)[1].lower()
|
| 64 |
+
for file_type, extensions in FileProcessor.ALLOWED_EXTENSIONS.items():
|
| 65 |
+
if ext in extensions:
|
| 66 |
+
return file_type
|
| 67 |
+
return None
|
| 68 |
+
|
| 69 |
+
@staticmethod
|
| 70 |
+
async def process_file(file) -> str:
|
| 71 |
+
try:
|
| 72 |
+
file_type = FileProcessor.get_file_type(file.name)
|
| 73 |
+
|
| 74 |
+
if file_type == 'image':
|
| 75 |
+
return await FileProcessor.process_image(file)
|
| 76 |
+
elif file_type == 'text':
|
| 77 |
+
return await FileProcessor.process_text(file)
|
| 78 |
+
else:
|
| 79 |
+
return "Unsupported file type"
|
| 80 |
+
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return f"Error processing file: {str(e)}"
|
| 83 |
+
|
| 84 |
+
@staticmethod
|
| 85 |
+
async def process_image(file):
|
| 86 |
+
try:
|
| 87 |
+
image = Image.open(file)
|
| 88 |
+
|
| 89 |
+
# Claude API๋ฅผ ํตํ ์ด๋ฏธ์ง ๋ถ์
|
| 90 |
+
image_bytes = io.BytesIO()
|
| 91 |
+
image.save(image_bytes, format=image.format)
|
| 92 |
+
image_base64 = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
|
| 93 |
+
|
| 94 |
+
messages = [
|
| 95 |
+
{
|
| 96 |
+
"role": "user",
|
| 97 |
+
"content": [
|
| 98 |
+
{
|
| 99 |
+
"type": "image",
|
| 100 |
+
"source": {
|
| 101 |
+
"type": "base64",
|
| 102 |
+
"media_type": f"image/{image.format.lower()}",
|
| 103 |
+
"data": image_base64
|
| 104 |
+
}
|
| 105 |
+
},
|
| 106 |
+
{
|
| 107 |
+
"type": "text",
|
| 108 |
+
"text": "Please analyze this image and provide a detailed description."
|
| 109 |
+
}
|
| 110 |
+
]
|
| 111 |
+
}
|
| 112 |
+
]
|
| 113 |
+
|
| 114 |
+
response = claude_client.messages.create(
|
| 115 |
+
model="claude-3-opus-20240229",
|
| 116 |
+
max_tokens=1000,
|
| 117 |
+
messages=messages
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
return response.content[0].text
|
| 121 |
+
|
| 122 |
+
except Exception as e:
|
| 123 |
+
return f"Error processing image: {str(e)}"
|
| 124 |
+
|
| 125 |
+
@staticmethod
|
| 126 |
+
async def process_text(file):
|
| 127 |
+
try:
|
| 128 |
+
text_content = ""
|
| 129 |
+
ext = os.path.splitext(file.name)[1].lower()
|
| 130 |
+
|
| 131 |
+
if ext == '.pdf':
|
| 132 |
+
pdf = fitz.open(stream=file.read(), filetype="pdf")
|
| 133 |
+
for page in pdf:
|
| 134 |
+
text_content += page.get_text()
|
| 135 |
+
elif ext == '.docx':
|
| 136 |
+
doc = docx.Document(file)
|
| 137 |
+
text_content = "\n".join([paragraph.text for paragraph in doc.paragraphs])
|
| 138 |
+
else: # .txt, .rtf
|
| 139 |
+
text_content = file.read().decode('utf-8')
|
| 140 |
+
|
| 141 |
+
# Claude API๋ฅผ ํตํ ํ
์คํธ ๋ถ์
|
| 142 |
+
response = claude_client.messages.create(
|
| 143 |
+
model="claude-3-opus-20240229",
|
| 144 |
+
max_tokens=1000,
|
| 145 |
+
messages=[
|
| 146 |
+
{
|
| 147 |
+
"role": "user",
|
| 148 |
+
"content": f"Please analyze this text and provide a summary and key insights:\n\n{text_content[:2000]}..."
|
| 149 |
+
}
|
| 150 |
+
]
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
return response.content[0].text
|
| 154 |
+
|
| 155 |
+
except Exception as e:
|
| 156 |
+
return f"Error processing text file: {str(e)}"
|
| 157 |
+
|
| 158 |
# ์บ์ ๊ฒฝ๋ก ์ค์
|
| 159 |
cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
|
| 160 |
os.environ["TRANSFORMERS_CACHE"] = cache_path
|
|
|
|
| 813 |
.custom-button:hover {
|
| 814 |
background: #0056b3;
|
| 815 |
}
|
| 816 |
+
|
| 817 |
+
|
| 818 |
+
.file-upload {
|
| 819 |
+
border: 2px dashed #007aff;
|
| 820 |
+
border-radius: 10px;
|
| 821 |
+
padding: 20px;
|
| 822 |
+
text-align: center;
|
| 823 |
+
background: #f8f9fa;
|
| 824 |
+
transition: all 0.3s ease;
|
| 825 |
+
}
|
| 826 |
+
|
| 827 |
+
.file-upload:hover {
|
| 828 |
+
border-color: #0056b3;
|
| 829 |
+
background: #e9ecef;
|
| 830 |
+
}
|
| 831 |
+
|
| 832 |
+
.analysis-result {
|
| 833 |
+
margin-top: 20px;
|
| 834 |
+
padding: 15px;
|
| 835 |
+
border-radius: 8px;
|
| 836 |
+
background: #fff;
|
| 837 |
+
border: 1px solid #e0e0e0;
|
| 838 |
+
max-height: 300px;
|
| 839 |
+
overflow-y: auto;
|
| 840 |
+
}
|
| 841 |
+
|
| 842 |
+
.analyze-btn {
|
| 843 |
+
margin-top: 10px;
|
| 844 |
+
background: #28c840 !important;
|
| 845 |
+
border-color: #28c840 !important;
|
| 846 |
+
}
|
| 847 |
+
|
| 848 |
+
.analyze-btn:hover {
|
| 849 |
+
background: #23a836 !important;
|
| 850 |
+
border-color: #23a836 !important;
|
| 851 |
+
}
|
| 852 |
""", theme=theme)
|
| 853 |
|
| 854 |
|
|
|
|
| 1018 |
# ์ข์ธก ํจ๋
|
| 1019 |
with antd.Col(span=24, md=8):
|
| 1020 |
with antd.Flex(vertical=True, gap="middle", wrap=True):
|
| 1021 |
+
|
| 1022 |
+
|
| 1023 |
+
|
| 1024 |
# macOS ์คํ์ผ ์๋์ฐ ํค๋
|
| 1025 |
header = gr.HTML("""
|
| 1026 |
<div class="window-frame">
|
|
|
|
| 1046 |
</div>
|
| 1047 |
""".format(get_image_base64('mouse.gif')))
|
| 1048 |
|
| 1049 |
+
# ํ์ผ ์
๋ก๋ ์น์
|
| 1050 |
+
file_upload = gr.File(
|
| 1051 |
+
label="Upload Files (Images/Documents)",
|
| 1052 |
+
file_types=[".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp",
|
| 1053 |
+
".txt", ".pdf", ".docx", ".rtf"],
|
| 1054 |
+
file_count="multiple",
|
| 1055 |
+
elem_classes="file-upload"
|
| 1056 |
+
)
|
| 1057 |
+
|
| 1058 |
+
file_analysis = gr.Textbox(
|
| 1059 |
+
label="File Analysis Result",
|
| 1060 |
+
interactive=False,
|
| 1061 |
+
elem_classes="analysis-result"
|
| 1062 |
+
)
|
| 1063 |
+
|
| 1064 |
+
analyze_btn = antd.Button(
|
| 1065 |
+
"Analyze Files",
|
| 1066 |
+
type="primary",
|
| 1067 |
+
size="large",
|
| 1068 |
+
elem_classes="analyze-btn"
|
| 1069 |
+
)
|
| 1070 |
+
|
| 1071 |
+
# ์
๋ ฅ ์์ญ
|
| 1072 |
input = antd.InputTextarea(
|
| 1073 |
size="large",
|
| 1074 |
allow_clear=True,
|
| 1075 |
placeholder=get_random_placeholder(),
|
| 1076 |
+
elem_classes="custom-textarea"
|
|
|
|
| 1077 |
)
|
| 1078 |
+
|
| 1079 |
+
# ๋ฒํผ ๊ทธ๋ฃน
|
| 1080 |
with antd.Flex(gap="small", justify="flex-start"):
|
| 1081 |
btn = antd.Button(
|
| 1082 |
"Generate",
|
|
|
|
| 1097 |
elem_classes="share-btn"
|
| 1098 |
)
|
| 1099 |
|
| 1100 |
+
deploy_result = gr.HTML(
|
| 1101 |
+
label="Share Result",
|
| 1102 |
+
elem_classes="deploy-result"
|
| 1103 |
+
)
|
| 1104 |
|
| 1105 |
+
|
| 1106 |
+
# ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ค์ ํ ๊ณณ์ ๋ชจ์์ ์ ์
|
| 1107 |
+
async def handle_file_upload(files):
|
| 1108 |
+
if not files:
|
| 1109 |
+
return "No files uploaded"
|
| 1110 |
+
|
| 1111 |
+
if len(files) > FileProcessor.MAX_FILES:
|
| 1112 |
+
return f"Maximum {FileProcessor.MAX_FILES} files allowed"
|
| 1113 |
+
|
| 1114 |
+
results = []
|
| 1115 |
+
for file in files:
|
| 1116 |
+
if not FileProcessor.is_allowed_file(file.name):
|
| 1117 |
+
results.append(f"Unsupported file: {file.name}")
|
| 1118 |
+
continue
|
| 1119 |
+
|
| 1120 |
+
if os.path.getsize(file.name) > FileProcessor.MAX_FILE_SIZE:
|
| 1121 |
+
results.append(f"File too large: {file.name}")
|
| 1122 |
+
continue
|
| 1123 |
+
|
| 1124 |
+
result = await FileProcessor.process_file(file)
|
| 1125 |
+
results.append(f"Analysis for {file.name}:\n{result}\n")
|
| 1126 |
+
|
| 1127 |
+
return "\n".join(results)
|
| 1128 |
+
|
| 1129 |
+
# ์ด๋ฒคํธ ํธ๋ค๋ฌ ์ฐ๊ฒฐ
|
| 1130 |
+
analyze_btn.click(
|
| 1131 |
+
fn=handle_file_upload,
|
| 1132 |
+
inputs=[file_upload],
|
| 1133 |
+
outputs=[file_analysis]
|
| 1134 |
+
)
|
| 1135 |
+
|
| 1136 |
+
btn.click(
|
| 1137 |
+
demo_instance.generation_code,
|
| 1138 |
+
inputs=[input, setting],
|
| 1139 |
+
outputs=[code_output, sandbox, state_tab, code_drawer]
|
| 1140 |
+
).then(
|
| 1141 |
+
fn=update_placeholder,
|
| 1142 |
+
inputs=[],
|
| 1143 |
+
outputs=[input]
|
| 1144 |
+
)
|
| 1145 |
+
|
| 1146 |
+
boost_btn.click(
|
| 1147 |
+
fn=handle_boost,
|
| 1148 |
+
inputs=[input],
|
| 1149 |
+
outputs=[input, state_tab]
|
| 1150 |
+
)
|
| 1151 |
+
|
| 1152 |
+
deploy_btn.click(
|
| 1153 |
+
fn=lambda code: deploy_to_vercel(remove_code_block(code)) if code else "No code to share.",
|
| 1154 |
+
inputs=[code_output],
|
| 1155 |
+
outputs=[deploy_result]
|
| 1156 |
+
)
|
| 1157 |
|
| 1158 |
# ์ฐ์ธก ํจ๋
|
| 1159 |
with antd.Col(span=24, md=16):
|