kaitongg commited on
Commit
203f314
·
verified ·
1 Parent(s): 3c080b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -26
app.py CHANGED
@@ -27,7 +27,7 @@ num_classes = bundle["num_classes"]
27
  class_names = bundle["class_names"]
28
  state_dict = bundle["state_dict"]
29
 
30
- device = "cuda" if torch.cuda.is_available() else "cpu"
31
  model = timm.create_model(architecture, pretrained=False, num_classes=num_classes)
32
  model.load_state_dict(state_dict)
33
  model.eval().to(device)
@@ -110,10 +110,10 @@ def perform_text_classification_and_format(text: str):
110
 
111
  predicted_text_label = str(loaded_predictor_from_hub.predict(text_df_processed).iloc[0])
112
  if predicted_text_label == "1":
113
- has_high_concept = ""
114
  confidence = text_classification_probabilities["High Concept"]
115
  else:
116
- has_high_concept = ""
117
  confidence = text_classification_probabilities["No High Concept"]
118
 
119
  text_classification_formatted = f"High Concept: {has_high_concept} (Confidence: {confidence:.2f})"
@@ -141,27 +141,28 @@ def generate_prompt_only(image_classification_results, text_classification_proba
141
  if image_classification_results and "error" not in image_classification_results:
142
  design_stage = max(image_classification_results, key=image_classification_results.get)
143
 
144
- has_high_concept = ""
145
  confidence = text_classification_probabilities.get("No High Concept", 0.0)
146
  if predicted_text_label == "1":
147
- has_high_concept = ""
148
  confidence = text_classification_probabilities.get("High Concept", 0.0)
149
 
150
  llm_attitude = llm_attitude_mapping.get(design_stage, llm_attitude_mapping["random"])
151
 
152
  prompt = f"""You are an abstract architecture critique interpreter.
153
  Your audience is a low-level architecture student.
154
- 已知用户处于{design_stage}设计阶段,所以你的态度应该要{llm_attitude}
155
- 已知用户输入的结果(是/否)含有抽象建筑学概念:{has_high_concept}
156
- 牢记规则:
157
- - 撰写一段英文,严格控制在250-350字。
158
- - 文末必须以完整句子收尾。
159
- - 不得重复任何观点或句子。
160
- - 禁止使用警句、口号或平行句式。
161
- - 不得出现“最终输出”、‘输出结束’、“无后续文本”等元注释。
162
- - 禁止添加自我反思或系统性备注。
163
- - 段落末句结束后立即终止输出。
164
- 以下是用户输入的文本内容:{text}你需要用儿童都懂的语言,举生活中的例子给用户解释抽象概念,并且给出可操作的建议。
 
165
  """
166
  return prompt
167
 
@@ -191,16 +192,20 @@ with gr.Blocks() as demo_step_by_step:
191
  gr.Markdown("# Architecture Feedback Generator (Step-by-Step)")
192
 
193
  with gr.Row():
194
- image_input = gr.Image(type="pil", label="Upload Architectural Image")
195
- text_input = gr.Textbox(label="Enter Text Description or Question")
196
-
197
- classify_button = gr.Button("Perform Classification & Generate Prompt")
198
- image_output_label = gr.Label(num_top_classes=len(class_names), label="Image Classification Results")
199
- text_output_textbox = gr.Textbox(label="Text Classification Results")
200
- text_classification_probabilities_state = gr.State()
201
- prompt_output_textbox = gr.Textbox(label="Generated Prompt for LLM", interactive=True)
202
- generate_feedback_button = gr.Button("Generate Feedback from Prompt")
203
- llm_output_text = gr.Textbox(label="Generated Feedback")
 
 
 
 
204
 
205
  def dynamic_generate_prompt(img_res, txt_prob, txt):
206
  predicted_label = "1" if txt_prob.get("High Concept",0) > txt_prob.get("No High Concept",0) else "0"
 
27
  class_names = bundle["class_names"]
28
  state_dict = bundle["state_dict"]
29
 
30
+ device = "cpu"
31
  model = timm.create_model(architecture, pretrained=False, num_classes=num_classes)
32
  model.load_state_dict(state_dict)
33
  model.eval().to(device)
 
110
 
111
  predicted_text_label = str(loaded_predictor_from_hub.predict(text_df_processed).iloc[0])
112
  if predicted_text_label == "1":
113
+ has_high_concept = "Yes"
114
  confidence = text_classification_probabilities["High Concept"]
115
  else:
116
+ has_high_concept = "No"
117
  confidence = text_classification_probabilities["No High Concept"]
118
 
119
  text_classification_formatted = f"High Concept: {has_high_concept} (Confidence: {confidence:.2f})"
 
141
  if image_classification_results and "error" not in image_classification_results:
142
  design_stage = max(image_classification_results, key=image_classification_results.get)
143
 
144
+ has_high_concept = "No"
145
  confidence = text_classification_probabilities.get("No High Concept", 0.0)
146
  if predicted_text_label == "1":
147
+ has_high_concept = "Yes"
148
  confidence = text_classification_probabilities.get("High Concept", 0.0)
149
 
150
  llm_attitude = llm_attitude_mapping.get(design_stage, llm_attitude_mapping["random"])
151
 
152
  prompt = f"""You are an abstract architecture critique interpreter.
153
  Your audience is a low-level architecture student.
154
+ The user is currently in the {design_stage} design stage, so your attitude should be {llm_attitude}.
155
+ The user's input contains abstract architectural concepts (Yes/No): {has_high_concept}.
156
+ Rules:
157
+ - Write a paragraph in English, strictly between 250-350 words.
158
+ - End with a complete sentence.
159
+ - Do not repeat any ideas or sentences.
160
+ - Do not use slogans, mottos, or parallel structures.
161
+ - Do not include phrases like 'final output', 'end of output', or meta-comments.
162
+ - Do not add self-reflection or systematic remarks.
163
+ - Stop immediately after the last sentence of the paragraph.
164
+ Here is the user input text: {text}
165
+ You must use simple language that a child could understand, provide everyday life examples to explain abstract concepts, and give actionable suggestions.
166
  """
167
  return prompt
168
 
 
192
  gr.Markdown("# Architecture Feedback Generator (Step-by-Step)")
193
 
194
  with gr.Row():
195
+ with gr.Column():
196
+ image_input = gr.Image(type="pil", label="Upload Architectural Image")
197
+ text_input = gr.Textbox(label="Enter Text Description or Question", lines=4)
198
+ classify_button = gr.Button("Perform Classification & Generate Prompt")
199
+
200
+ with gr.Column():
201
+ image_output_label = gr.Label(num_top_classes=len(class_names), label="Image Classification Results")
202
+ text_output_textbox = gr.Textbox(label="Text Classification Results", lines=2)
203
+ text_classification_probabilities_state = gr.State()
204
+ prompt_output_textbox = gr.Textbox(label="Generated Prompt for LLM", interactive=True, lines=8)
205
+ generate_feedback_button = gr.Button("Generate Feedback from Prompt")
206
+
207
+ with gr.Column():
208
+ llm_output_text = gr.Textbox(label="Generated Feedback", lines=12)
209
 
210
  def dynamic_generate_prompt(img_res, txt_prob, txt):
211
  predicted_label = "1" if txt_prob.get("High Concept",0) > txt_prob.get("No High Concept",0) else "0"