Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,96 +3,92 @@ import time
|
|
| 3 |
import google.generativeai as genai
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
-
# Set your Gemini API key
|
| 7 |
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
|
| 8 |
|
| 9 |
-
|
| 10 |
-
"""Uploads the given file to Gemini."""
|
| 11 |
-
file = genai.upload_file(path, mime_type=mime_type)
|
| 12 |
-
print(f"Uploaded file '{file.display_name}' as: {file.uri}")
|
| 13 |
-
return file
|
| 14 |
-
|
| 15 |
-
def wait_for_files_active(files):
|
| 16 |
-
"""Waits for the given files to be active."""
|
| 17 |
-
print("Waiting for file processing...")
|
| 18 |
-
for name in (file.name for file in files):
|
| 19 |
-
file = genai.get_file(name)
|
| 20 |
-
while file.state.name == "PROCESSING":
|
| 21 |
-
print(".", end="", flush=True)
|
| 22 |
-
time.sleep(10)
|
| 23 |
-
file = genai.get_file(name)
|
| 24 |
-
if file.state.name != "ACTIVE":
|
| 25 |
-
raise Exception(f"File {file.name} failed to process")
|
| 26 |
-
print("...all files ready")
|
| 27 |
-
print()
|
| 28 |
-
|
| 29 |
-
# Create the model
|
| 30 |
generation_config = {
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
}
|
| 37 |
-
|
| 38 |
model = genai.GenerativeModel(
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
system_instruction="건축가, 건축 디자이너, 학교공간혁신 전문가, 공간 및 건축관련 잡지 에디터",
|
| 42 |
)
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
if path: # Check if a file was actually uploaded
|
| 49 |
-
files.append(upload_to_gemini(path, mime_type="image/png")) # Assuming PNG for now
|
| 50 |
|
| 51 |
-
|
| 52 |
-
return "이미지를 업로드해주세요."
|
| 53 |
|
| 54 |
-
|
| 55 |
-
wait_for_files_active(files)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
prompt = "두개의 사진 자료를 참고해서 잡지 기획 기사처럼 만들어봐, 절대 교육청 홍보자료나 보고서처럼 보이면 안되고 세련된 잡지, 좋은 카피들을 좀 중간중간에 넣어서, 홈베이스 특성, 조성된 공간의 포인트 등을 잘 살려서. "
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
prompt += ", ".join(space_characteristics)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
"role": "user",
|
| 68 |
-
"parts": [
|
| 69 |
-
prompt,
|
| 70 |
-
*files # Add all uploaded files to the prompt
|
| 71 |
-
],
|
| 72 |
-
}
|
| 73 |
-
]
|
| 74 |
-
)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
|
| 80 |
|
| 81 |
-
# Gradio 인터페이스
|
| 82 |
iface = gr.Interface(
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
description="사진을 업로드하면 공간에 대한 기사를 생성해줍니다.",
|
| 96 |
)
|
| 97 |
|
| 98 |
# 인터페이스 실행
|
|
|
|
| 3 |
import google.generativeai as genai
|
| 4 |
import gradio as gr
|
| 5 |
|
|
|
|
| 6 |
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
|
| 7 |
|
| 8 |
+
# Gemini 모델 설정
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
generation_config = {
|
| 10 |
+
"temperature": 0.7,
|
| 11 |
+
"top_p": 0.95,
|
| 12 |
+
"top_k": 64,
|
| 13 |
+
"max_output_tokens": 5000,
|
| 14 |
+
"response_mime_type": "text/plain",
|
| 15 |
}
|
|
|
|
| 16 |
model = genai.GenerativeModel(
|
| 17 |
+
model_name="gemini-1.5-pro-002",
|
| 18 |
+
generation_config=generation_config,
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
+
def generate_news_article(images, space_characteristics):
|
| 22 |
+
"""
|
| 23 |
+
학교 공간 혁신 사진과 공간 특성 요소를 기반으로 기사문을 생성합니다.
|
| 24 |
+
|
| 25 |
+
Args:
|
| 26 |
+
images: 학교 공간 혁신 사진들 (gradio.Image 리스트 타입)
|
| 27 |
+
space_characteristics: 선택된 공간 특성 요소들 (리스트 타입)
|
| 28 |
+
|
| 29 |
+
Returns:
|
| 30 |
+
str: 생성된 기사문 텍스트 (한국어)
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
# 초기 빈 텍스트 출력
|
| 34 |
+
full_text = ""
|
| 35 |
+
yield full_text
|
| 36 |
+
|
| 37 |
+
# 사진 설명 생성
|
| 38 |
+
image_descriptions = []
|
| 39 |
+
for i, image in enumerate(images):
|
| 40 |
+
image_descriptions.append(f"사진 {i+1}: {image}")
|
| 41 |
+
|
| 42 |
+
# 공간 특성 요소 설명 추가
|
| 43 |
+
characteristics_description = "\n**공간 특성:**\n"
|
| 44 |
+
for characteristic in space_characteristics:
|
| 45 |
+
if characteristic == "유연하다":
|
| 46 |
+
characteristics_description += "- 하나의 공간을 목적에 따라 다양한 공간으로 사용할 수 있도록 디자인\n"
|
| 47 |
+
elif characteristic == "연결하다":
|
| 48 |
+
characteristics_description += "- 공간과 공간, 사람과 사람 간 유기적인 교류와 소통을 지원하고, 정보 접근과 활용이 원활한 공간으로 디자인\n"
|
| 49 |
+
elif characteristic == "상상하다":
|
| 50 |
+
characteristics_description += "- 흥미와 호기심, 상상력을 자극하고, 모험적이고 놀이적 요소가 많아서 다양하고 흥미로운 경험을 제공할 수 있도록 디자인\n"
|
| 51 |
+
elif characteristic == "특화하다":
|
| 52 |
+
characteristics_description += "- 사용자가 원하는 유형의 활동(놀이, 학습, 실험, 표현 및 전시, 휴식 등)을 충분히 지원할 수 있도록 디자인\n"
|
| 53 |
|
| 54 |
+
response = model.generate_content([
|
| 55 |
+
f"""## 학교 공간 혁신, 미래 교육 환경 조성
|
| 56 |
+
**사진과 선택된 공간 특성 요소들을 참고하여 학교 공간 혁신에 대한 기사문을 작성해주세요.**
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
{chr(10).join(image_descriptions)}
|
|
|
|
| 59 |
|
| 60 |
+
{characteristics_description}
|
|
|
|
| 61 |
|
| 62 |
+
**기사 작성 시 다음 내용을 포함해주세요:**
|
|
|
|
| 63 |
|
| 64 |
+
* 혁신된 공간에 대한 구체적인 설명 (예: 스마트 교실, 협력 학습 공간, 메이커 스페이스 등)
|
| 65 |
+
* 공간 특성 요소가 잘 드러난 설명
|
|
|
|
| 66 |
|
| 67 |
+
**전문적인 기사 형식을 갖춰 작성해주세요.**""",
|
| 68 |
+
*images
|
| 69 |
+
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
# 전체 텍스트를 점진적으로 생성
|
| 72 |
+
for chunk in response.text:
|
| 73 |
+
full_text += chunk
|
| 74 |
+
yield full_text
|
| 75 |
+
time.sleep(0.01)
|
| 76 |
|
| 77 |
|
| 78 |
+
# Gradio 인터페이스 설정
|
| 79 |
iface = gr.Interface(
|
| 80 |
+
fn=generate_news_article,
|
| 81 |
+
inputs=[
|
| 82 |
+
gr.Gallery(label="학교 공간 혁신 사진들").style(grid=[2, 2]),
|
| 83 |
+
gr.Dropdown(
|
| 84 |
+
choices=["유연하다", "연결하다", "상상하다", "특화하다"],
|
| 85 |
+
label="공간 특성 요소",
|
| 86 |
+
multiselect=True
|
| 87 |
+
)
|
| 88 |
+
],
|
| 89 |
+
outputs=gr.Textbox(label="생성된 기사문"),
|
| 90 |
+
title="학교 공간 혁신 기사 생성",
|
| 91 |
+
description="학교 공간 혁신 사진과 공간 특성 요소를 선택하여 기사문을 생성합니다.",
|
|
|
|
| 92 |
)
|
| 93 |
|
| 94 |
# 인터페이스 실행
|