Spaces:
Running
Running
Download app.py from Dane351/VehicIdent-AI: direct link, hf CLI and curl.
- Browser
- Download file 1.39 kB
-
https://huggingface.co/spaces/Dane351/VehicIdent-AI/resolve/main/app.py
- Command line
-
hf download hf://spaces/Dane351/VehicIdent-AI/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Dane351/VehicIdent-AI/resolve/main/app.py
1.39 kB
| import openai | |
| import gradio as gr | |
| import base64 | |
| from io import BytesIO | |
| from PIL import Image | |
| import os | |
| # Load your OpenAI API key | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| # Function to convert image to base64 | |
| def image_to_base64(image): | |
| buffered = BytesIO() | |
| image.save(buffered, format="PNG") | |
| return base64.b64encode(buffered.getvalue()).decode("utf-8") | |
| # Vehicle identifier | |
| def identify_vehicle(image): | |
| try: | |
| base64_img = image_to_base64(image) | |
| response = openai.chat.completions.create( | |
| model="gpt-4-turbo", | |
| messages=[ | |
| { | |
| "role": "user", | |
| "content": [ | |
| {"type": "text", "text": "What vehicle or part is this? Be specific."}, | |
| {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_img}"}} | |
| ] | |
| } | |
| ], | |
| max_tokens=300 | |
| ) | |
| return response.choices[0].message.content | |
| except Exception as e: | |
| return f"Error: {e}" | |
| # Gradio UI | |
| interface = gr.Interface( | |
| fn=identify_vehicle, | |
| inputs=gr.Image(type="pil", label="Upload Image"), | |
| outputs="text", | |
| title="AutoIQ", | |
| description="Upload a photo of a vehicle or part and get an instant ID." | |
| ) | |
| if __name__ == "__main__": | |
| interface.launch() | |
| interface.launch(share=True) |