Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -433,25 +433,103 @@ async def try_openai_api(openai_messages):
|
|
433 |
|
434 |
class Demo:
|
435 |
def __init__(self):
|
436 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
437 |
|
438 |
async def generation_code(self, query: Optional[str], _setting: Dict[str, str]):
|
439 |
if not query or query.strip() == '':
|
440 |
query = get_random_placeholder()
|
441 |
|
442 |
-
# ํ์ผ ๋ถ์
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
# ์ด๋ฏธ์ง ์์ฑ์ด ํ์ํ์ง ํ์ธ
|
451 |
needs_image = '์ด๋ฏธ์ง' in query or '๊ทธ๋ฆผ' in query or 'image' in query.lower()
|
452 |
image_prompt = None
|
453 |
-
|
454 |
-
# ์ด๋ฏธ์ง ํ๋กฌํํธ ์ถ์ถ
|
455 |
if needs_image:
|
456 |
for keyword in ['์ด๋ฏธ์ง:', '๊ทธ๋ฆผ:', 'image:']:
|
457 |
if keyword in query.lower():
|
@@ -459,18 +537,18 @@ class Demo:
|
|
459 |
break
|
460 |
if not image_prompt:
|
461 |
image_prompt = query
|
462 |
-
|
463 |
messages = [{'role': Role.SYSTEM, 'content': _setting['system']}]
|
464 |
messages.append({'role': Role.USER, 'content': query})
|
465 |
|
466 |
system_message = messages[0]['content']
|
467 |
claude_messages = [{"role": "user", "content": query}]
|
468 |
-
|
469 |
-
# ์
๋ก๋๋ ์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ Claude API ๋ฉ์์ง์
|
470 |
-
for filename, file in self.
|
471 |
-
if
|
472 |
try:
|
473 |
-
image = Image.open(file)
|
474 |
image_bytes = io.BytesIO()
|
475 |
image.save(image_bytes, format=image.format)
|
476 |
image_base64 = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
|
@@ -490,7 +568,8 @@ class Demo:
|
|
490 |
}
|
491 |
]
|
492 |
except Exception as e:
|
493 |
-
print(f"Error
|
|
|
494 |
|
495 |
openai_messages = [
|
496 |
{"role": "system", "content": system_message},
|
@@ -1207,43 +1286,20 @@ Use the "Generate" button for basic creation, "Enhance" button for prompt improv
|
|
1207 |
with antd.Tabs.Item(key="render"):
|
1208 |
sandbox = gr.HTML(elem_classes="html_content")
|
1209 |
|
1210 |
-
|
1211 |
-
|
1212 |
-
return "No files uploaded"
|
1213 |
-
|
1214 |
-
if len(files) > FileProcessor.MAX_FILES:
|
1215 |
-
return f"Maximum {FileProcessor.MAX_FILES} files allowed"
|
1216 |
-
|
1217 |
-
results = []
|
1218 |
-
file_state.last_analysis.clear()
|
1219 |
-
file_state.uploaded_files.clear()
|
1220 |
-
|
1221 |
-
for file in files:
|
1222 |
-
if not FileProcessor.is_allowed_file(file.name):
|
1223 |
-
results.append(f"Unsupported file: {file.name}")
|
1224 |
-
continue
|
1225 |
-
|
1226 |
-
if os.path.getsize(file.name) > FileProcessor.MAX_FILE_SIZE:
|
1227 |
-
results.append(f"File too large: {file.name}")
|
1228 |
-
continue
|
1229 |
-
|
1230 |
-
result = await FileProcessor.process_file(file)
|
1231 |
-
file_state.last_analysis[file.name] = result
|
1232 |
-
file_state.uploaded_files[file.name] = file
|
1233 |
-
results.append(f"Analysis for {file.name}:\n{result}\n")
|
1234 |
-
|
1235 |
-
return "\n".join(results)
|
1236 |
-
|
1237 |
|
1238 |
-
|
|
|
1239 |
analyze_btn.click(
|
1240 |
-
fn=handle_file_upload,
|
1241 |
inputs=[file_upload],
|
1242 |
outputs=[file_analysis]
|
1243 |
)
|
1244 |
|
|
|
1245 |
btn.click(
|
1246 |
-
demo_instance.generation_code,
|
1247 |
inputs=[input, setting],
|
1248 |
outputs=[code_output, sandbox, state_tab, code_drawer]
|
1249 |
).then(
|
@@ -1251,6 +1307,7 @@ Use the "Generate" button for basic creation, "Enhance" button for prompt improv
|
|
1251 |
inputs=[],
|
1252 |
outputs=[input]
|
1253 |
)
|
|
|
1254 |
|
1255 |
boost_btn.click(
|
1256 |
fn=handle_boost,
|
|
|
433 |
|
434 |
class Demo:
|
435 |
def __init__(self):
|
436 |
+
self.file_analysis = {} # ํ์ผ ๋ถ์ ๊ฒฐ๊ณผ ์ ์ฅ
|
437 |
+
self.uploaded_files = {} # ์
๋ก๋๋ ํ์ผ ๊ฐ์ฒด ์ ์ฅ
|
438 |
+
|
439 |
+
async def handle_file_upload(self, files):
|
440 |
+
"""ํ์ผ ์
๋ก๋ ์ฒ๋ฆฌ ํจ์"""
|
441 |
+
if not files:
|
442 |
+
return "No files uploaded"
|
443 |
+
|
444 |
+
results = []
|
445 |
+
self.file_analysis.clear()
|
446 |
+
self.uploaded_files.clear()
|
447 |
+
|
448 |
+
for file in files:
|
449 |
+
try:
|
450 |
+
# ์ด๋ฏธ์ง ํ์ผ ์ฒ๋ฆฌ
|
451 |
+
if file.name.lower().endswith(('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp')):
|
452 |
+
image = Image.open(file.name)
|
453 |
+
image_bytes = io.BytesIO()
|
454 |
+
image.save(image_bytes, format=image.format)
|
455 |
+
image_base64 = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
|
456 |
+
|
457 |
+
# Claude API๋ก ์ด๋ฏธ์ง ๋ถ์
|
458 |
+
response = claude_client.messages.create(
|
459 |
+
model="claude-3-opus-20240229",
|
460 |
+
max_tokens=1000,
|
461 |
+
messages=[{
|
462 |
+
"role": "user",
|
463 |
+
"content": [
|
464 |
+
{
|
465 |
+
"type": "image",
|
466 |
+
"source": {
|
467 |
+
"type": "base64",
|
468 |
+
"media_type": f"image/{image.format.lower()}",
|
469 |
+
"data": image_base64
|
470 |
+
}
|
471 |
+
},
|
472 |
+
{
|
473 |
+
"type": "text",
|
474 |
+
"text": "Please analyze this image and provide a detailed description."
|
475 |
+
}
|
476 |
+
]
|
477 |
+
}]
|
478 |
+
)
|
479 |
+
analysis = response.content[0].text
|
480 |
+
|
481 |
+
# ํ
์คํธ ํ์ผ ์ฒ๋ฆฌ
|
482 |
+
elif file.name.lower().endswith(('.txt', '.pdf', '.docx', '.rtf')):
|
483 |
+
if file.name.lower().endswith('.pdf'):
|
484 |
+
pdf = fitz.open(file.name)
|
485 |
+
text_content = ""
|
486 |
+
for page in pdf:
|
487 |
+
text_content += page.get_text()
|
488 |
+
elif file.name.lower().endswith('.docx'):
|
489 |
+
doc = docx.Document(file.name)
|
490 |
+
text_content = "\n".join([paragraph.text for paragraph in doc.paragraphs])
|
491 |
+
else:
|
492 |
+
with open(file.name, 'r', encoding='utf-8') as f:
|
493 |
+
text_content = f.read()
|
494 |
+
|
495 |
+
# Claude API๋ก ํ
์คํธ ๋ถ์
|
496 |
+
response = claude_client.messages.create(
|
497 |
+
model="claude-3-opus-20240229",
|
498 |
+
max_tokens=1000,
|
499 |
+
messages=[{
|
500 |
+
"role": "user",
|
501 |
+
"content": f"Please analyze this text and provide a summary:\n\n{text_content[:2000]}..."
|
502 |
+
}]
|
503 |
+
)
|
504 |
+
analysis = response.content[0].text
|
505 |
+
|
506 |
+
else:
|
507 |
+
analysis = f"Unsupported file type: {file.name}"
|
508 |
+
|
509 |
+
# ๋ถ์ ๊ฒฐ๊ณผ ์ ์ฅ
|
510 |
+
self.file_analysis[file.name] = analysis
|
511 |
+
self.uploaded_files[file.name] = file
|
512 |
+
results.append(f"Analysis for {file.name}:\n{analysis}\n")
|
513 |
+
|
514 |
+
except Exception as e:
|
515 |
+
results.append(f"Error processing {file.name}: {str(e)}")
|
516 |
+
|
517 |
+
return "\n".join(results)
|
518 |
|
519 |
async def generation_code(self, query: Optional[str], _setting: Dict[str, str]):
|
520 |
if not query or query.strip() == '':
|
521 |
query = get_random_placeholder()
|
522 |
|
523 |
+
# ํ์ผ ๋ถ์ ๊ฒฐ๊ณผ๋ฅผ ์ฟผ๋ฆฌ์ ํฌํจ
|
524 |
+
if self.file_analysis:
|
525 |
+
context = "Based on the analyzed files:\n\n"
|
526 |
+
for filename, analysis in self.file_analysis.items():
|
527 |
+
context += f"File '{filename}':\n{analysis}\n\n"
|
528 |
+
query = f"{context}\nUser Query: {query}"
|
529 |
+
|
530 |
+
# ์ด๋ฏธ์ง ์์ฑ ๊ด๋ จ ์ฝ๋๋ ์ ์ง
|
|
|
531 |
needs_image = '์ด๋ฏธ์ง' in query or '๊ทธ๋ฆผ' in query or 'image' in query.lower()
|
532 |
image_prompt = None
|
|
|
|
|
533 |
if needs_image:
|
534 |
for keyword in ['์ด๋ฏธ์ง:', '๊ทธ๋ฆผ:', 'image:']:
|
535 |
if keyword in query.lower():
|
|
|
537 |
break
|
538 |
if not image_prompt:
|
539 |
image_prompt = query
|
540 |
+
|
541 |
messages = [{'role': Role.SYSTEM, 'content': _setting['system']}]
|
542 |
messages.append({'role': Role.USER, 'content': query})
|
543 |
|
544 |
system_message = messages[0]['content']
|
545 |
claude_messages = [{"role": "user", "content": query}]
|
546 |
+
|
547 |
+
# ์
๋ก๋๋ ์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ Claude API ๋ฉ์์ง์ ํฌํจ
|
548 |
+
for filename, file in self.uploaded_files.items():
|
549 |
+
if filename.lower().endswith(('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp')):
|
550 |
try:
|
551 |
+
image = Image.open(file.name)
|
552 |
image_bytes = io.BytesIO()
|
553 |
image.save(image_bytes, format=image.format)
|
554 |
image_base64 = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
|
|
|
568 |
}
|
569 |
]
|
570 |
except Exception as e:
|
571 |
+
print(f"Error including image in message: {str(e)}")
|
572 |
+
|
573 |
|
574 |
openai_messages = [
|
575 |
{"role": "system", "content": system_message},
|
|
|
1286 |
with antd.Tabs.Item(key="render"):
|
1287 |
sandbox = gr.HTML(elem_classes="html_content")
|
1288 |
|
1289 |
+
|
1290 |
+
demo_instance = Demo() # Demo ์ธ์คํด์ค ์์ฑ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1291 |
|
1292 |
+
|
1293 |
+
# ํ์ผ ์
๋ก๋ ๋ฒํผ ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
1294 |
analyze_btn.click(
|
1295 |
+
fn=demo_instance.handle_file_upload, # Demo ์ธ์คํด์ค์ ๋ฉ์๋ ์ฌ์ฉ
|
1296 |
inputs=[file_upload],
|
1297 |
outputs=[file_analysis]
|
1298 |
)
|
1299 |
|
1300 |
+
# Generate ๋ฒํผ ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
1301 |
btn.click(
|
1302 |
+
fn=demo_instance.generation_code,
|
1303 |
inputs=[input, setting],
|
1304 |
outputs=[code_output, sandbox, state_tab, code_drawer]
|
1305 |
).then(
|
|
|
1307 |
inputs=[],
|
1308 |
outputs=[input]
|
1309 |
)
|
1310 |
+
|
1311 |
|
1312 |
boost_btn.click(
|
1313 |
fn=handle_boost,
|