K00B404 commited on
Commit
0be8e3e
·
verified ·
1 Parent(s): 370a0c1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -0
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import transformers
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ from PIL import Image
5
+ import warnings
6
+ import gradio as gr
7
+ import os
8
+
9
+ # Disable warnings for cleaner output
10
+ transformers.logging.set_verbosity_error()
11
+ transformers.logging.disable_progress_bar()
12
+ warnings.filterwarnings('ignore')
13
+
14
+ # Set device - will use CUDA if available, otherwise CPU
15
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
16
+ torch.set_default_device(device)
17
+
18
+ # Model configuration
19
+ model_name = 'qnguyen3/nanoLLaVA-1.5'
20
+
21
+ print(f"Loading model {model_name} on {device}...")
22
+
23
+ # Create model
24
+ model = AutoModelForCausalLM.from_pretrained(
25
+ model_name,
26
+ torch_dtype=torch.float16,
27
+ device_map='auto',
28
+ trust_remote_code=True)
29
+ tokenizer = AutoTokenizer.from_pretrained(
30
+ model_name,
31
+ trust_remote_code=True)
32
+
33
+ print("Model loaded successfully!")
34
+
35
+ def analyze_character(image_path, analysis_type):
36
+ """
37
+ Analyze a character image for dramaturgical insights
38
+
39
+ Args:
40
+ image_path: Path to the character image
41
+ analysis_type: Type of character analysis to perform
42
+
43
+ Returns:
44
+ str: The generated character analysis
45
+ """
46
+ # Load and process image
47
+ try:
48
+ image = Image.open(image_path).convert('RGB')
49
+ image_tensor = model.process_images([image], model.config).to(dtype=model.dtype)
50
+ except Exception as e:
51
+ return f"Error processing image: {str(e)}"
52
+
53
+ # Create prompt based on analysis type
54
+ if analysis_type == "full_analysis":
55
+ prompt = ("Analyze this character as a dramaturg would. Describe their appearance, "
56
+ "potential personality traits, character archetype, suitable roles, and how they might "
57
+ "function within a dramatic narrative. Consider costume, posture, expression, and visual symbolism.")
58
+ elif analysis_type == "archetype":
59
+ prompt = ("Identify the potential character archetype(s) represented in this image. "
60
+ "Consider both classical archetypes (hero, mentor, trickster, etc.) and modern "
61
+ "interpretations. Explain your reasoning based on visual cues.")
62
+ elif analysis_type == "historical_context":
63
+ prompt = ("Analyze this character's appearance in terms of historical context. "
64
+ "Identify the likely time period, cultural influences, and how these elements "
65
+ "would influence the character's role in a dramatic work. Consider costume details, "
66
+ "props, and stylistic elements.")
67
+ else:
68
+ prompt = "Describe this character in detail for dramatic casting purposes."
69
+
70
+ # Format input for the model using ChatML format
71
+ messages = [
72
+ {"role": "system", "content": "You are an expert dramaturg with deep knowledge of character analysis, theatrical traditions, and visual storytelling."},
73
+ {"role": "user", "content": f'<image>\n{prompt}'}
74
+ ]
75
+
76
+ text = tokenizer.apply_chat_template(
77
+ messages,
78
+ tokenize=False,
79
+ add_generation_prompt=True
80
+ )
81
+
82
+ # Split text around image placeholder
83
+ text_chunks = [tokenizer(chunk).input_ids for chunk in text.split('<image>')]
84
+ input_ids = torch.tensor(text_chunks[0] + [-200] + text_chunks[1], dtype=torch.long).unsqueeze(0)
85
+
86
+ # Generate response
87
+ try:
88
+ output_ids = model.generate(
89
+ input_ids,
90
+ images=image_tensor,
91
+ max_new_tokens=1024,
92
+ temperature=0.7,
93
+ top_p=0.9,
94
+ use_cache=True)[0]
95
+
96
+ response = tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip()
97
+ return response
98
+ except Exception as e:
99
+ return f"Error generating analysis: {str(e)}"
100
+
101
+ # Create Gradio interface
102
+ def create_ui():
103
+ with gr.Blocks(title="Dramaturg Character Analyzer") as demo:
104
+ gr.Markdown("# Dramaturg Character Analyzer")
105
+ gr.Markdown("Upload a character image to receive a dramaturgical analysis")
106
+
107
+ with gr.Row():
108
+ with gr.Column():
109
+ input_image = gr.Image(type="filepath", label="Upload Character Image")
110
+ analysis_type = gr.Radio(
111
+ ["full_analysis", "archetype", "historical_context", "basic_description"],
112
+ label="Analysis Type",
113
+ value="full_analysis"
114
+ )
115
+ analyze_btn = gr.Button("Analyze Character")
116
+
117
+ with gr.Column():
118
+ output_text = gr.Textbox(label="Character Analysis", lines=20)
119
+
120
+ analyze_btn.click(
121
+ fn=analyze_character,
122
+ inputs=[input_image, analysis_type],
123
+ outputs=output_text
124
+ )
125
+
126
+ return demo
127
+
128
+ # Main function
129
+ if __name__ == "__main__":
130
+ demo = create_ui()
131
+ demo.launch(share=True)
132
+ print("Dramaturg Character Analyzer is now running!")