mxiean commited on
Commit
03e7c37
·
verified ·
1 Parent(s): 05a91c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -31
app.py CHANGED
@@ -2,49 +2,41 @@ import gradio as gr
2
  from transformers import pipeline
3
  from datasets import load_dataset
4
 
5
- # Load dataset and models
6
- dataset = load_dataset("AntZet/home_decoration_objects_images")
7
- style_clf = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
8
- advice_gen = pipeline("text2text-generation", model="google/flan-t5-base")
9
 
10
- def get_recommendation(style_input):
11
- # 1. Find matching styles
12
- style_examples = [d for d in dataset['train'] if d['style'].lower() == style_input.lower()]
13
-
14
- if not style_examples:
15
- return "Style not found. Try: Industrial, Scandinavian, etc."
16
-
17
- # 2. Get common objects
18
- objects = [obj for ex in style_examples for obj in ex['objects']]
19
- top_objects = sorted(set(objects), key=lambda x: -objects.count(x))[:3]
20
 
21
- # 3. Generate advice
22
- prompt = f"""As an Airbnb host, how to decorate in {style_input} style?
23
- Key objects: {top_objects}
24
- Provide:
25
- - 3 essential elements
26
- - 2 budget tips
27
- - Colors to use"""
28
 
29
- advice = advice_gen(prompt, max_length=300)[0]['generated_text']
 
 
30
 
31
- # 4. Show example images
32
- example_images = [ex['image'] for ex in style_examples[:3]]
33
 
34
  return advice, example_images
35
 
36
- # Simple UI
37
  with gr.Blocks() as demo:
38
- gr.Markdown("## 🏡 Airbnb Style Advisor")
39
- style_input = gr.Textbox(label="Enter style (e.g. Industrial)")
40
- submit_btn = gr.Button("Get Advice")
41
 
42
  with gr.Row():
43
- advice_output = gr.Textbox(label="Recommendations")
44
- gallery = gr.Gallery(label="Style Examples")
45
 
46
  submit_btn.click(
47
- fn=get_recommendation,
48
  inputs=style_input,
49
  outputs=[advice_output, gallery]
50
  )
 
2
  from transformers import pipeline
3
  from datasets import load_dataset
4
 
5
+ # 加载数据集
6
+ dataset = load_dataset("AntZet/home_decoration_objects_images", streaming=True)
7
+ dataset = dataset['train'].take(100) # 只取前100个样本加快加载
 
8
 
9
+ # 初始化模型
10
+ style_advisor = pipeline("text-generation", model="gpt2") # 使用较小的gpt2模型
11
+
12
+ def get_advice(style):
13
+ # 从数据集中找出匹配风格的例子
14
+ examples = [ex for ex in dataset if ex['style'].lower() == style.lower()]
 
 
 
 
15
 
16
+ if not examples:
17
+ return "未找到该风格,请尝试:工业风、北欧风等", []
 
 
 
 
 
18
 
19
+ # 生成建议
20
+ prompt = f"如何将Airbnb房间装修成{style}风格?请给出3条具体建议"
21
+ advice = style_advisor(prompt, max_length=150)[0]['generated_text']
22
 
23
+ # 获取示例图片
24
+ example_images = [ex['image'] for ex in examples[:3]]
25
 
26
  return advice, example_images
27
 
28
+ # 创建界面
29
  with gr.Blocks() as demo:
30
+ gr.Markdown("## 🏡 Airbnb装修助手")
31
+ style_input = gr.Textbox(label="输入想要的风格")
32
+ submit_btn = gr.Button("生成建议")
33
 
34
  with gr.Row():
35
+ advice_output = gr.Textbox(label="装修建议")
36
+ gallery = gr.Gallery(label="风格示例")
37
 
38
  submit_btn.click(
39
+ fn=get_advice,
40
  inputs=style_input,
41
  outputs=[advice_output, gallery]
42
  )