openfree commited on
Commit
3dc0b12
ยท
verified ยท
1 Parent(s): 9be5bee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +180 -176
app.py CHANGED
@@ -433,197 +433,201 @@ async def try_openai_api(openai_messages):
433
 
434
  class Demo:
435
  def __init__(self):
436
- # file_state ๋Œ€์‹  ์ง์ ‘ ์†์„ฑ์œผ๋กœ ์ •์˜
437
- self.last_analysis = {} # ํŒŒ์ผ ๋ถ„์„ ๊ฒฐ๊ณผ ์ €์žฅ
438
- self.uploaded_files = {} # ์—…๋กœ๋“œ๋œ ํŒŒ์ผ ๊ฐ์ฒด ์ €์žฅ
439
 
440
  async def generation_code(self, query: Optional[str], _setting: Dict[str, str]):
441
- if not query or query.strip() == '':
442
- query = get_random_placeholder()
443
-
444
- # ํŒŒ์ผ ๋ถ„์„ ๊ฒฐ๊ณผ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์ปจํ…์ŠคํŠธ๋กœ ์ถ”๊ฐ€
445
- context = ""
446
- if self.last_analysis: # file_state.last_analysis ๋Œ€์‹  ์ง์ ‘ ์ฐธ์กฐ
447
- context = "Based on the uploaded files:\n"
448
- for filename, analysis in self.last_analysis.items():
449
- context += f"\nFile '{filename}':\n{analysis}\n"
450
- query = f"{context}\n\nUser Query: {query}"
451
-
452
- # ์ด๋ฏธ์ง€ ์ƒ์„ฑ์ด ํ•„์š”ํ•œ์ง€ ํ™•์ธ
453
- needs_image = '์ด๋ฏธ์ง€' in query or '๊ทธ๋ฆผ' in query or 'image' in query.lower()
454
- image_prompt = None
455
-
456
- if needs_image:
457
- for keyword in ['์ด๋ฏธ์ง€:', '๊ทธ๋ฆผ:', 'image:']:
458
- if keyword in query.lower():
459
- image_prompt = query.split(keyword)[1].strip()
460
- break
461
- if not image_prompt:
462
- image_prompt = query
463
-
464
- messages = [{'role': Role.SYSTEM, 'content': _setting['system']}]
465
- messages.append({'role': Role.USER, 'content': query})
466
-
467
- system_message = messages[0]['content']
468
- claude_messages = [{"role": "user", "content": query}]
 
469
 
470
- # ์—…๋กœ๋“œ๋œ ์ด๋ฏธ์ง€๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ Claude API ๋ฉ”์‹œ์ง€์— ์ถ”๊ฐ€
471
- for filename, file in self.uploaded_files.items(): # file_state.uploaded_files ๋Œ€์‹  ์ง์ ‘ ์ฐธ์กฐ
472
- if any(filename.lower().endswith(ext) for ext in ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp']):
473
- try:
474
- image = Image.open(file)
475
- image_bytes = io.BytesIO()
476
- image.save(image_bytes, format=image.format)
477
- image_base64 = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
478
-
479
- claude_messages[0]["content"] = [
480
- {
481
- "type": "image",
482
- "source": {
483
- "type": "base64",
484
- "media_type": f"image/{image.format.lower()}",
485
- "data": image_base64
 
 
 
 
 
486
  }
487
- },
488
- {
489
- "type": "text",
490
- "text": query
491
- }
492
- ]
493
- except Exception as e:
494
- print(f"Error processing uploaded image: {str(e)}")
495
 
496
- openai_messages = [
497
- {"role": "system", "content": system_message},
498
- {"role": "user", "content": query}
499
- ]
500
-
501
- try:
502
- yield [
503
- "",
504
- None,
505
- gr.update(active_key="loading"),
506
- gr.update(open=True)
507
  ]
508
- await asyncio.sleep(0)
509
-
510
- collected_content = None
511
  try:
512
- async for content in try_claude_api(system_message, claude_messages):
513
- yield [
514
- "",
515
- None,
516
- gr.update(active_key="loading"),
517
- gr.update(open=True)
518
- ]
519
- await asyncio.sleep(0)
520
- collected_content = content
521
-
522
- except Exception as claude_error:
523
- print(f"Falling back to OpenAI API due to Claude error: {str(claude_error)}")
524
-
525
- async for content in try_openai_api(openai_messages):
526
- yield [
527
- "",
528
- None,
529
- gr.update(active_key="loading"),
530
- gr.update(open=True)
531
- ]
532
- await asyncio.sleep(0)
533
- collected_content = content
534
 
535
- if collected_content:
536
- # FLUX๋ฅผ ์‚ฌ์šฉํ•œ ์ด๋ฏธ์ง€ ์ƒ์„ฑ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ
537
- if needs_image and image_prompt:
538
- try:
539
- print(f"Generating image for prompt: {image_prompt}")
540
- if pipe is not None:
541
- image = generate_image(
542
- prompt=image_prompt,
543
- height=512,
544
- width=512,
545
- steps=8,
546
- scales=3.5,
547
- seed=random.randint(1, 10000)
548
- )
549
-
550
- buffered = BytesIO()
551
- image.save(buffered, format="PNG")
552
- img_str = base64.b64encode(buffered.getvalue()).decode()
553
-
554
- image_html = f'''
555
- <div class="generated-image" style="margin: 20px 0; text-align: center;">
556
- <h3 style="color: #333; margin-bottom: 10px;">Generated Image:</h3>
557
- <img src="data:image/png;base64,{img_str}"
558
- style="max-width: 100%;
559
- border-radius: 10px;
560
- box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
561
- <p style="color: #666; margin-top: 10px; font-style: italic;">
562
- Prompt: {html.escape(image_prompt)}
563
- </p>
564
- </div>
565
- '''
566
-
567
- if '```html' in collected_content:
568
- collected_content = collected_content.replace('```html\n', f'```html\n{image_html}')
569
- else:
570
- collected_content = f'```html\n{image_html}\n```\n{collected_content}'
571
-
572
- print("Image generation successful")
573
- else:
574
- raise Exception("FLUX model not initialized")
575
-
576
- except Exception as e:
577
- print(f"Image generation error: {str(e)}")
578
- error_message = f'''
579
- <div style="color: #ff4d4f; padding: 10px; margin: 10px 0;
580
- border-left: 4px solid #ff4d4f; background: #fff2f0;">
581
- <p>Failed to generate image: {str(e)}</p>
582
- </div>
583
- '''
584
- if '```html' in collected_content:
585
- collected_content = collected_content.replace('```html\n', f'```html\n{error_message}')
586
- else:
587
- collected_content = f'```html\n{error_message}\n```\n{collected_content}'
588
-
589
- # ์—…๋กœ๋“œ๋œ ์ด๋ฏธ์ง€ ํ‘œ์‹œ
590
- for filename, file in self.file_state.uploaded_files.items():
591
- if any(filename.lower().endswith(ext) for ext in ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp']):
592
  try:
593
- image = Image.open(file)
594
- buffered = BytesIO()
595
- image.save(buffered, format=image.format)
596
- img_str = base64.b64encode(buffered.getvalue()).decode()
597
-
598
- uploaded_image_html = f'''
599
- <div class="uploaded-image" style="margin: 20px 0; text-align: center;">
600
- <h3 style="color: #333; margin-bottom: 10px;">Uploaded Image: {html.escape(filename)}</h3>
601
- <img src="data:image/{image.format.lower()};base64,{img_str}"
602
- style="max-width: 100%;
603
- border-radius: 10px;
604
- box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
605
  </div>
606
  '''
607
-
608
  if '```html' in collected_content:
609
- collected_content = collected_content.replace('```html\n', f'```html\n{uploaded_image_html}')
610
  else:
611
- collected_content = f'```html\n{uploaded_image_html}\n```\n{collected_content}'
612
- except Exception as e:
613
- print(f"Error displaying uploaded image: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
614
 
615
- yield [
616
- collected_content,
617
- send_to_sandbox(remove_code_block(collected_content)),
618
- gr.update(active_key="render"),
619
- gr.update(open=False)
620
- ]
621
- else:
622
- raise ValueError("No content was generated from either API")
623
-
624
  except Exception as e:
625
- print(f"Error details: {str(e)}")
626
- raise ValueError(f'Error calling APIs: {str(e)}')
627
 
628
  async def handle_file_upload(self, files):
629
  """ํŒŒ์ผ ์—…๋กœ๋“œ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜"""
 
433
 
434
  class Demo:
435
  def __init__(self):
436
+ self.last_analysis = {}
437
+ self.uploaded_files = {}
 
438
 
439
  async def generation_code(self, query: Optional[str], _setting: Dict[str, str]):
440
+ try:
441
+ if not query or query.strip() == '':
442
+ query = get_random_placeholder()
443
+
444
+ # ํŒŒ์ผ ๋ถ„์„ ๊ฒฐ๊ณผ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ์ปจํ…์ŠคํŠธ๋กœ ์ถ”๊ฐ€
445
+ context = ""
446
+ if self.last_analysis: # self.file_state ๋Œ€์‹  ์ง์ ‘ self.last_analysis ์‚ฌ์šฉ
447
+ context = "Based on the uploaded files:\n"
448
+ for filename, analysis in self.last_analysis.items():
449
+ context += f"\nFile '{filename}':\n{analysis}\n"
450
+ query = f"{context}\n\nUser Query: {query}"
451
+
452
+ # ์ด๋ฏธ์ง€ ์ƒ์„ฑ์ด ํ•„์š”ํ•œ์ง€ ํ™•์ธ
453
+ needs_image = '์ด๋ฏธ์ง€' in query or '๊ทธ๋ฆผ' in query or 'image' in query.lower()
454
+ image_prompt = None
455
+
456
+ if needs_image:
457
+ for keyword in ['์ด๋ฏธ์ง€:', '๊ทธ๋ฆผ:', 'image:']:
458
+ if keyword in query.lower():
459
+ image_prompt = query.split(keyword)[1].strip()
460
+ break
461
+ if not image_prompt:
462
+ image_prompt = query
463
+
464
+ messages = [{'role': Role.SYSTEM, 'content': _setting['system']}]
465
+ messages.append({'role': Role.USER, 'content': query})
466
+
467
+ system_message = messages[0]['content']
468
+ claude_messages = [{"role": "user", "content": query}]
469
 
470
+ # ์—…๋กœ๋“œ๋œ ์ด๋ฏธ์ง€๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ Claude API ๋ฉ”์‹œ์ง€์— ์ถ”๊ฐ€
471
+ for filename, file in self.uploaded_files.items(): # self.file_state ๋Œ€์‹  ์ง์ ‘ self.uploaded_files ์‚ฌ์šฉ
472
+ if any(filename.lower().endswith(ext) for ext in ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp']):
473
+ try:
474
+ image = Image.open(file)
475
+ image_bytes = io.BytesIO()
476
+ image.save(image_bytes, format=image.format)
477
+ image_base64 = base64.b64encode(image_bytes.getvalue()).decode('utf-8')
478
+
479
+ claude_messages[0]["content"] = [
480
+ {
481
+ "type": "image",
482
+ "source": {
483
+ "type": "base64",
484
+ "media_type": f"image/{image.format.lower()}",
485
+ "data": image_base64
486
+ }
487
+ },
488
+ {
489
+ "type": "text",
490
+ "text": query
491
  }
492
+ ]
493
+ except Exception as e:
494
+ print(f"Error processing uploaded image: {str(e)}")
 
 
 
 
 
495
 
496
+ openai_messages = [
497
+ {"role": "system", "content": system_message},
498
+ {"role": "user", "content": query}
 
 
 
 
 
 
 
 
499
  ]
500
+
 
 
501
  try:
502
+ yield [
503
+ "",
504
+ None,
505
+ gr.update(active_key="loading"),
506
+ gr.update(open=True)
507
+ ]
508
+ await asyncio.sleep(0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
509
 
510
+ collected_content = None
511
+ try:
512
+ async for content in try_claude_api(system_message, claude_messages):
513
+ yield [
514
+ "",
515
+ None,
516
+ gr.update(active_key="loading"),
517
+ gr.update(open=True)
518
+ ]
519
+ await asyncio.sleep(0)
520
+ collected_content = content
521
+
522
+ except Exception as claude_error:
523
+ print(f"Falling back to OpenAI API due to Claude error: {str(claude_error)}")
524
+
525
+ async for content in try_openai_api(openai_messages):
526
+ yield [
527
+ "",
528
+ None,
529
+ gr.update(active_key="loading"),
530
+ gr.update(open=True)
531
+ ]
532
+ await asyncio.sleep(0)
533
+ collected_content = content
534
+
535
+ if collected_content:
536
+ # ์ด๋ฏธ์ง€ ์ƒ์„ฑ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ
537
+ if needs_image and image_prompt:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
538
  try:
539
+ print(f"Generating image for prompt: {image_prompt}")
540
+ if pipe is not None:
541
+ image = generate_image(
542
+ prompt=image_prompt,
543
+ height=512,
544
+ width=512,
545
+ steps=8,
546
+ scales=3.5,
547
+ seed=random.randint(1, 10000)
548
+ )
549
+
550
+ buffered = BytesIO()
551
+ image.save(buffered, format="PNG")
552
+ img_str = base64.b64encode(buffered.getvalue()).decode()
553
+
554
+ image_html = f'''
555
+ <div class="generated-image" style="margin: 20px 0; text-align: center;">
556
+ <h3 style="color: #333; margin-bottom: 10px;">Generated Image:</h3>
557
+ <img src="data:image/png;base64,{img_str}"
558
+ style="max-width: 100%;
559
+ border-radius: 10px;
560
+ box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
561
+ <p style="color: #666; margin-top: 10px; font-style: italic;">
562
+ Prompt: {html.escape(image_prompt)}
563
+ </p>
564
+ </div>
565
+ '''
566
+
567
+ if '```html' in collected_content:
568
+ collected_content = collected_content.replace('```html\n', f'```html\n{image_html}')
569
+ else:
570
+ collected_content = f'```html\n{image_html}\n```\n{collected_content}'
571
+
572
+ print("Image generation successful")
573
+ else:
574
+ raise Exception("FLUX model not initialized")
575
+
576
+ except Exception as e:
577
+ print(f"Image generation error: {str(e)}")
578
+ error_message = f'''
579
+ <div style="color: #ff4d4f; padding: 10px; margin: 10px 0;
580
+ border-left: 4px solid #ff4d4f; background: #fff2f0;">
581
+ <p>Failed to generate image: {str(e)}</p>
582
  </div>
583
  '''
 
584
  if '```html' in collected_content:
585
+ collected_content = collected_content.replace('```html\n', f'```html\n{error_message}')
586
  else:
587
+ collected_content = f'```html\n{error_message}\n```\n{collected_content}'
588
+
589
+ # ์—…๋กœ๋“œ๋œ ์ด๋ฏธ์ง€ ํ‘œ์‹œ
590
+ for filename, file in self.uploaded_files.items():
591
+ if any(filename.lower().endswith(ext) for ext in ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp']):
592
+ try:
593
+ image = Image.open(file)
594
+ buffered = BytesIO()
595
+ image.save(buffered, format=image.format)
596
+ img_str = base64.b64encode(buffered.getvalue()).decode()
597
+
598
+ uploaded_image_html = f'''
599
+ <div class="uploaded-image" style="margin: 20px 0; text-align: center;">
600
+ <h3 style="color: #333; margin-bottom: 10px;">Uploaded Image: {html.escape(filename)}</h3>
601
+ <img src="data:image/{image.format.lower()};base64,{img_str}"
602
+ style="max-width: 100%;
603
+ border-radius: 10px;
604
+ box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
605
+ </div>
606
+ '''
607
+
608
+ if '```html' in collected_content:
609
+ collected_content = collected_content.replace('```html\n', f'```html\n{uploaded_image_html}')
610
+ else:
611
+ collected_content = f'```html\n{uploaded_image_html}\n```\n{collected_content}'
612
+ except Exception as e:
613
+ print(f"Error displaying uploaded image: {str(e)}")
614
+
615
+ yield [
616
+ collected_content,
617
+ send_to_sandbox(remove_code_block(collected_content)),
618
+ gr.update(active_key="render"),
619
+ gr.update(open=False)
620
+ ]
621
+ else:
622
+ raise ValueError("No content was generated from either API")
623
+
624
+ except Exception as e:
625
+ print(f"Error details: {str(e)}")
626
+ raise ValueError(f'Error calling APIs: {str(e)}')
627
 
 
 
 
 
 
 
 
 
 
628
  except Exception as e:
629
+ print(f"Error in generation_code: {str(e)}")
630
+ raise e
631
 
632
  async def handle_file_upload(self, files):
633
  """ํŒŒ์ผ ์—…๋กœ๋“œ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜"""