gopichandra commited on
Commit
29b77bd
·
verified ·
1 Parent(s): 6501751

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from PIL import Image
4
+ import requests
5
+
6
+ # Get the Hugging Face API token from the environment variable
7
+ HF_API_TOKEN = os.getenv("HF_API_TOKEN")
8
+ HF_API_URL = "https://api-inference.huggingface.co/models/google/vit-base-patch16-224"
9
+
10
+ def predict(image):
11
+ """
12
+ Sends the image to the Hugging Face model API for prediction.
13
+ """
14
+ headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
15
+ # Convert the image to a format accepted by the model
16
+ payload = {"inputs": image}
17
+
18
+ try:
19
+ response = requests.post(HF_API_URL, headers=headers, json=payload)
20
+ response.raise_for_status()
21
+ return response.json()
22
+ except requests.exceptions.RequestException as e:
23
+ return f"Error: {e}"
24
+
25
+ # Create a Gradio interface
26
+ iface = gr.Interface(
27
+ fn=predict,
28
+ inputs=gr.Image(type="filepath"), # Accepts an image file
29
+ outputs="text", # Outputs the prediction result as text
30
+ title="Hugging Face Camera Integration",
31
+ description="Upload an image to get predictions from the Hugging Face model."
32
+ )
33
+
34
+ # Launch the app
35
+ if __name__ == "__main__":
36
+ iface.launch()