gizemsarsinlar commited on
Commit
dea2ee7
·
verified ·
1 Parent(s): 688b560

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +7 -5
  2. app.py +97 -0
  3. gitattributes +35 -0
  4. requirements.txt +11 -0
README.md CHANGED
@@ -1,12 +1,14 @@
1
  ---
2
- title: Art Analysis With Phi-4
3
- emoji: 🐢
4
- colorFrom: purple
5
- colorTo: gray
6
  sdk: gradio
7
- sdk_version: 5.20.1
8
  app_file: app.py
9
  pinned: false
 
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Phi4 Multimodal
3
+ emoji: 🦀
4
+ colorFrom: yellow
5
+ colorTo: pink
6
  sdk: gradio
7
+ sdk_version: 5.19.0
8
  app_file: app.py
9
  pinned: false
10
+ license: apache-2.0
11
+ short_description: Space demoing Phi4 MultiModal
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import torch
4
+ from transformers import AutoModelForCausalLM, AutoProcessor
5
+ import spaces
6
+
7
+ # Define model path
8
+ model_path = "microsoft/Phi-4-multimodal-instruct"
9
+
10
+ # Load model and processor
11
+ processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
12
+ model = AutoModelForCausalLM.from_pretrained(
13
+ model_path,
14
+ device_map="auto",
15
+ torch_dtype="auto",
16
+ trust_remote_code=True,
17
+ _attn_implementation="eager",
18
+ )
19
+
20
+ # Define prompt structure
21
+ user_prompt = "<|user|>"
22
+ assistant_prompt = "<|assistant|>"
23
+ prompt_suffix = "<|end|>"
24
+
25
+ # Define inference function
26
+ @spaces.GPU
27
+ def analyze_artwork(file):
28
+ if not file:
29
+ return "Please upload an image of an artwork."
30
+
31
+ # Custom prompt for artwork analysis
32
+ prompt = (
33
+ f"{user_prompt} You are an expert art historian and critic. Your task is to analyze the given artwork."
34
+ f" Provide a structured and insightful analysis based on the following points:\n\n"
35
+ f"1. **General Description**: Describe the colors, composition, and subject.\n"
36
+ f"2. **Artistic Style**: Identify the artistic movement (e.g., Impressionism, Surrealism).\n"
37
+ f"3. **Historical Context**: Discuss the period (e.g., Renaissance, Baroque) and cultural influences that influenced the artwork.\n"
38
+ f"4. **Symbolism & Meaning**: Interpret the messages and emotions conveyed.\n"
39
+ f"5. **Technical Analysis**: Examine brushwork, lighting, and composition.\n"
40
+ f"6. **Impact & Significance**: Explain the artwork’s relevance in the art world.\n\n"
41
+ f"Here is the artwork for analysis:\n"
42
+ f"<|image_1|>\n"
43
+ f"{prompt_suffix}{assistant_prompt}"
44
+ )
45
+
46
+ # Open image from uploaded file
47
+ image = Image.open(file)
48
+ inputs = processor(text=prompt, images=image, return_tensors="pt").to(model.device)
49
+
50
+ # Generate response
51
+ with torch.no_grad():
52
+ generate_ids = model.generate(
53
+ **inputs,
54
+ max_new_tokens=1000,
55
+ num_logits_to_keep=0,
56
+ temperature=0.7,
57
+ top_k=50,
58
+ )
59
+ generate_ids = generate_ids[:, inputs["input_ids"].shape[1]:]
60
+ response = processor.batch_decode(
61
+ generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
62
+ )[0]
63
+
64
+ return response
65
+
66
+ # Gradio interface
67
+ with gr.Blocks(title="Art Analysis with Phi-4") as demo:
68
+ gr.Markdown(
69
+ """
70
+ # 🎨 Art Analysis with Phi-4
71
+ Upload an **image** of an artwork, and get a detailed analysis by an AI art historian!
72
+ Built with the microsoft/Phi-4-multimodal-instruct model.
73
+ """
74
+ )
75
+
76
+ with gr.Row():
77
+ with gr.Column(scale=1):
78
+ image_input = gr.Image(label="Upload Artwork Image", type="filepath")
79
+ submit_btn = gr.Button("Analyze Artwork", variant="primary")
80
+
81
+ with gr.Column(scale=2):
82
+ output_text = gr.Textbox(
83
+ label="Art Analysis Result",
84
+ placeholder="The AI's response will appear here...",
85
+ lines=20,
86
+ interactive=False,
87
+ )
88
+
89
+ # Connect the submit button
90
+ submit_btn.click(
91
+ fn=analyze_artwork,
92
+ inputs=[image_input],
93
+ outputs=output_text,
94
+ )
95
+
96
+ # Launch the demo
97
+ demo.launch()
gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ spaces
3
+ requests
4
+ torch
5
+ pillow
6
+ soundfile
7
+ transformers
8
+ torchvision
9
+ scipy
10
+ peft
11
+ backoff