oscurantismo commited on
Commit
ff3990a
·
verified ·
1 Parent(s): b2802b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -119
app.py CHANGED
@@ -1,124 +1,18 @@
1
- # Set OpenAI API Key
2
- openai.api_key = "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"
3
 
4
- # Load the CLIP model for object identification
5
- clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch16")
6
- clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch16")
7
 
8
- # Predefined object labels for CLIP
9
- object_labels = [
10
- "cat", "dog", "house", "tree", "car", "mountain", "flower", "bird", "person", "robot",
11
- "a digital artwork", "a portrait", "a landscape", "a futuristic cityscape"
12
- ]
13
 
14
- def analyze_contrast_opencv(image_path):
15
- """Analyze the contrast of the uploaded image using OpenCV."""
16
- img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
17
- contrast = img.std() # Standard deviation of pixel intensities
18
- return contrast
19
-
20
- def identify_objects_with_clip(image_path):
21
- """Identify objects in the image using CLIP."""
22
- image = Image.open(image_path).convert("RGB")
23
- inputs = clip_processor(text=object_labels, images=image, return_tensors="pt", padding=True)
24
-
25
- with torch.no_grad():
26
- outputs = clip_model(**inputs)
27
- logits_per_image = outputs.logits_per_image
28
- probs = logits_per_image.softmax(dim=1).numpy().flatten()
29
-
30
- # Get the label with the highest probability
31
- best_match_idx = probs.argmax()
32
- best_match_label = object_labels[best_match_idx]
33
- return best_match_label
34
-
35
- def enhance_contrast(image):
36
- """Enhance the contrast of the image."""
37
- enhancer = ImageEnhance.Contrast(image)
38
- enhanced_image = enhancer.enhance(2.0) # Increase contrast
39
- enhanced_path = "enhanced_image.png"
40
- enhanced_image.save(enhanced_path)
41
- return enhanced_path
42
-
43
- def suggest_improvements_with_chatgpt(object_name):
44
- """Generate improvement suggestions for the identified object using ChatGPT."""
45
- prompt = f"Suggest ways to improve a digital artwork featuring a {object_name}."
46
- response = openai.ChatCompletion.create(
47
- model="gpt-4",
48
- messages=[
49
- {"role": "system", "content": "You are a helpful assistant providing expert art improvement suggestions."},
50
- {"role": "user", "content": prompt}
51
- ]
52
- )
53
- return response["choices"][0]["message"]["content"]
54
-
55
- def diane_multimodal(message, history):
56
- """
57
- Process both text and image input:
58
- - Respond to text prompts about digital art.
59
- - Analyze image contrast, identify objects, and provide feedback.
60
- - Enhance contrast or suggest improvements when requested.
61
- """
62
- response = ""
63
- num_images = len(message["files"])
64
-
65
- if num_images > 0:
66
- # Handle uploaded images
67
- last_image_path = message["files"][-1]
68
- contrast = analyze_contrast_opencv(last_image_path)
69
- identified_object = identify_objects_with_clip(last_image_path)
70
-
71
- if contrast < 25: # Adjust threshold as needed
72
- response = (
73
- f"Hey, great drawing of a {identified_object}! However, it looks like the contrast is too low. "
74
- "Would you like me to improve it?"
75
- )
76
- # Save the analyzed image path to the history
77
- history.append({"role": "assistant", "content": (last_image_path,)})
78
- else:
79
- response = (
80
- f"Hey, great drawing of a {identified_object}! Looks like the color contrast is great, be proud of yourself :)"
81
- )
82
- return {"role": "assistant", "content": response}
83
-
84
- elif message["text"]:
85
- # Handle text-based inputs
86
- user_text = message["text"].lower()
87
- if any(keyword in user_text for keyword in ["improve", "yes", "better"]):
88
- # Check if an image was previously uploaded
89
- for entry in reversed(history):
90
- if isinstance(entry["content"], tuple): # Check for image in history
91
- last_image_path = entry["content"][0]
92
- enhanced_image_path = enhance_contrast(Image.open(last_image_path).convert("RGB"))
93
- # Return the text message first
94
- history.append({"role": "assistant", "content": "Here's your improved image! Let me know if you'd like further adjustments."})
95
- # Return the image in a separate message
96
- return {"role": "assistant", "content": gr.Image(enhanced_image_path)}
97
- elif "suggestions" in user_text:
98
- for entry in reversed(history):
99
- if isinstance(entry["content"], tuple): # Check for image in history
100
- last_image_path = entry["content"][0]
101
- identified_object = identify_objects_with_clip(last_image_path)
102
- improvement_suggestions = suggest_improvements_with_chatgpt(identified_object)
103
- return {"role": "assistant", "content": improvement_suggestions}
104
- else:
105
- response = "Feel free to upload an image or ask for art tips!"
106
-
107
- return {"role": "assistant", "content": response}
108
-
109
-
110
- # Define the multimodal chatbot interface
111
- demo = gr.ChatInterface(
112
- fn=diane_multimodal,
113
- type="messages",
114
- examples=[
115
- {"text": "Teach me about digital art tools", "files": []},
116
- {"text": "What is the best way to do shading digitally?", "files": []},
117
- ],
118
- multimodal=True,
119
- textbox=gr.MultimodalTextbox(file_count="multiple", file_types=["image"]),
120
- title="Your Digital Art Nice Expert (DIANE)",
121
- description="Use this chatbot to improve your digital art skills or analyze and enhance image contrast.",
122
  )
123
 
124
- demo.launch()
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
 
4
+ pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
 
 
5
 
6
+ def predict(input_img):
7
+ predictions = pipeline(input_img)
8
+ return input_img, {p["label"]: p["score"] for p in predictions}
 
 
9
 
10
+ gradio_app = gr.Interface(
11
+ predict,
12
+ inputs=gr.Image(label="Select hot dog candidate", sources=['upload', 'webcam'], type="pil"),
13
+ outputs=[gr.Image(label="Processed Image"), gr.Label(label="Result", num_top_classes=2)],
14
+ title="Hot Dog? Or Not?",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  )
16
 
17
+ if __name__ == "__main__":
18
+ gradio_app.launch()