Spaces:
Build error
Build error
Download image_processing.py from nextbeeh/DemoProject: direct link, hf CLI and curl.
- Browser
- Download file 2.18 kB
-
https://huggingface.co/spaces/nextbeeh/DemoProject/resolve/main/image_processing.py
- Command line
-
hf download hf://spaces/nextbeeh/DemoProject/image_processing.py
-
curl -L -o image_processing.py https://huggingface.co/spaces/nextbeeh/DemoProject/resolve/main/image_processing.py
2.18 kB
| import base64 | |
| import gradio as gr | |
| import vertexai | |
| from vertexai.generative_models import GenerativeModel, Part, FinishReason | |
| import vertexai.preview.generative_models as generative_models | |
| import io | |
| # Vertex AI & Gemini Setup | |
| vertexai.init(project="crack-root-420614", location="us-central1") | |
| model = GenerativeModel("gemini-experimental") | |
| generation_config = { | |
| "max_output_tokens": 8192, | |
| "temperature": 1, | |
| "top_p": 0.95, | |
| } | |
| safety_settings = { | |
| generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE, | |
| generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE, | |
| generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE, | |
| generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE, | |
| } | |
| def analyze_review(image, prompt): | |
| if image is not None: # Check if an image is provided (works for older Gradio) | |
| # Convert the PIL image to a base64 encoded string | |
| with io.BytesIO() as output: | |
| image.save(output, format="PNG") | |
| encoded_image = base64.b64encode(output.getvalue()).decode('ascii') | |
| image_part = Part.from_data(mime_type="image/png", data=encoded_image) | |
| parts = [image_part, prompt] | |
| else: | |
| parts = [prompt] | |
| responses = model.generate_content( | |
| parts, | |
| generation_config=generation_config, | |
| safety_settings=safety_settings, | |
| stream=True, | |
| ) | |
| generated_text = "" | |
| for response in responses: | |
| generated_text += response.text | |
| return generated_text | |
| # Gradio Interface | |
| iface = gr.Interface( | |
| fn=analyze_review, | |
| inputs=[ | |
| gr.Image(type="pil"), | |
| gr.Textbox(label="Enter your prompt", placeholder="Describe what you want the model to do with the uploaded image.") | |
| ], | |
| outputs=gr.Text(), | |
| title="Image Analyzer", | |
| description="Upload a image (optional) and enter a custom prompt to analyze its content using AI." | |
| ) | |
| iface.launch(share=True) | |