VinitT commited on
Commit
672864c
1 Parent(s): 0ec6ba8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from huggingface_hub import login
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
+ from PIL import Image
6
+ import requests
7
+ import torch
8
+
9
+ # Step 1: Log in to Hugging Face with your access token from secrets
10
+ huggingface_token = os.getenv("HUGGINGFACE_TOKEN") # Fetch the token from environment
11
+ if huggingface_token:
12
+ login(token=huggingface_token) # Authenticate using the token
13
+ else:
14
+ st.error("Hugging Face token not found. Please set it in the Secrets section.")
15
+
16
+ # Step 2: Load the model and tokenizer
17
+ model_name = "meta-llama/Llama-3.2-11B-Vision-Instruct" # Adjust if needed
18
+ try:
19
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
20
+ model = AutoModelForCausalLM.from_pretrained(model_name)
21
+ st.success("Model loaded successfully!")
22
+ except Exception as e:
23
+ st.error(f"Error loading model: {str(e)}")
24
+
25
+ # Step 3: Create a simple Streamlit app
26
+ def main():
27
+ st.title("Llama 3.2 11B Vision Model")
28
+ st.write("Upload an image and enter a prompt to generate output.")
29
+
30
+ # Upload image
31
+ image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
32
+ prompt = st.text_area("Enter your prompt here:")
33
+
34
+ if st.button("Generate Output"):
35
+ if image_file and prompt:
36
+ # Load image
37
+ image = Image.open(image_file)
38
+ st.image(image, caption="Uploaded Image", use_column_width=True)
39
+
40
+ # Preprocess the image if needed (convert to tensor, etc.)
41
+ # This depends on how the model expects the image input
42
+
43
+ # Example of converting image to a format suitable for the model
44
+ # Note: Adjust this part based on your model's requirements.
45
+ # Here, we're just using a placeholder for the model input.
46
+ # You might need to resize or normalize the image based on the model's requirements.
47
+ # For example:
48
+ # image_tensor = preprocess_image(image)
49
+
50
+ try:
51
+ # Prepare the input for the model
52
+ inputs = tokenizer(prompt, return_tensors='pt')
53
+
54
+ # Perform inference
55
+ # Adjust the input format for the model accordingly
56
+ # Here we assume the model takes a prompt and an image (adjust as necessary)
57
+ with torch.no_grad():
58
+ model_output = model.generate(**inputs) # Pass image tensor if required
59
+
60
+ # Decode the output
61
+ output_text = tokenizer.decode(model_output[0], skip_special_tokens=True)
62
+ st.write("Generated Output:", output_text)
63
+ except Exception as e:
64
+ st.error(f"Error during prediction: {str(e)}")
65
+ else:
66
+ st.warning("Please upload an image and enter a prompt.")
67
+
68
+ if __name__ == "__main__":
69
+ main()