Anil commited on
Commit
4ccf27c
·
1 Parent(s): 2e87189
Files changed (1) hide show
  1. image_processing.py +64 -0
image_processing.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import gradio as gr
3
+ import vertexai
4
+ from vertexai.generative_models import GenerativeModel, Part, FinishReason
5
+ import vertexai.preview.generative_models as generative_models
6
+ import io
7
+
8
+ # Vertex AI & Gemini Setup
9
+ vertexai.init(project="crack-root-420614", location="us-central1")
10
+ model = GenerativeModel("gemini-experimental")
11
+
12
+ generation_config = {
13
+ "max_output_tokens": 8192,
14
+ "temperature": 1,
15
+ "top_p": 0.95,
16
+ }
17
+
18
+ safety_settings = {
19
+ generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
20
+ generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
21
+ generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
22
+ generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
23
+ }
24
+
25
+
26
+ def analyze_review(image, prompt):
27
+ if image is not None: # Check if an image is provided (works for older Gradio)
28
+ # Convert the PIL image to a base64 encoded string
29
+ with io.BytesIO() as output:
30
+ image.save(output, format="PNG")
31
+ encoded_image = base64.b64encode(output.getvalue()).decode('ascii')
32
+
33
+ image_part = Part.from_data(mime_type="image/png", data=encoded_image)
34
+ parts = [image_part, prompt]
35
+
36
+ else:
37
+ parts = [prompt]
38
+
39
+ responses = model.generate_content(
40
+ parts,
41
+ generation_config=generation_config,
42
+ safety_settings=safety_settings,
43
+ stream=True,
44
+ )
45
+
46
+ generated_text = ""
47
+ for response in responses:
48
+ generated_text += response.text
49
+ return generated_text
50
+
51
+
52
+ # Gradio Interface
53
+ iface = gr.Interface(
54
+ fn=analyze_review,
55
+ inputs=[
56
+ gr.Image(type="pil"),
57
+ gr.Textbox(label="Enter your prompt", placeholder="Describe what you want the model to do with the uploaded image.")
58
+ ],
59
+ outputs=gr.Text(),
60
+ title="Image Analyzer",
61
+ description="Upload a image (optional) and enter a custom prompt to analyze its content using AI."
62
+ )
63
+
64
+ iface.launch(share=True)