Madhuri123 commited on
Commit
722c6e5
·
verified ·
1 Parent(s): 538b49d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -21
app.py CHANGED
@@ -1,34 +1,33 @@
1
  import streamlit as st
2
  import transformers
3
  import torch
 
 
 
4
 
5
  HF_TOKEN=st.secrets["hf_token"]
6
  # Load the model and pipeline
7
  model_id = "meta-llama/Llama-3.2-11B-Vision"
 
 
 
8
 
9
  # Set up the pipeline with the Hugging Face token
10
- pipeline = transformers.pipeline(
11
- "text-generation",
12
- model=model_id,
13
- model_kwargs={"torch_dtype": torch.bfloat16, "use_auth_token": HF_TOKEN}
14
  )
 
 
 
 
 
 
 
 
 
 
15
 
16
- # Streamlit user interface
17
- st.title("LLM Model Inference")
18
- st.write(f"**Using model:** {model_id}")
19
- input_text = st.text_input("Enter your prompt:")
20
-
21
- if st.button("Generate"):
22
- if input_text: # Check if the input is not empty
23
- # Generate text using the pipeline
24
- messages = [
25
- {"role": "system", "content": "You are a question answering assistant."},
26
- {"role": "user", "content": input_text}
27
- ]
28
- response = pipeline(messages, max_new_tokens=30)
29
- st.write("Generated Response:")
30
- st.write(response[0]['generated_text'][-1]['content'])
31
- else:
32
- st.error("Please enter a prompt to generate text.")
33
 
34
 
 
1
  import streamlit as st
2
  import transformers
3
  import torch
4
+ import requests
5
+ from PIL import Image
6
+ from transformers import MllamaForConditionalGeneration, AutoProcessor
7
 
8
  HF_TOKEN=st.secrets["hf_token"]
9
  # Load the model and pipeline
10
  model_id = "meta-llama/Llama-3.2-11B-Vision"
11
+ # Streamlit user interface
12
+ st.title("LLM Model Inference")
13
+ st.write(f"**Using model:** {model_id}")
14
 
15
  # Set up the pipeline with the Hugging Face token
16
+ model = MllamaForConditionalGeneration.from_pretrained(
17
+ model_id,
18
+ torch_dtype=torch.bfloat16,
19
+ device_map="auto",
20
  )
21
+ processor = AutoProcessor.from_pretrained(model_id)
22
+
23
+ url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/0052a70beed5bf71b92610a43a52df6d286cd5f3/diffusers/rabbit.jpg"
24
+ image = Image.open(requests.get(url, stream=True).raw)
25
+
26
+ prompt = "<|image|><|begin_of_text|>If I had to write a haiku for this one"
27
+ inputs = processor(image, prompt, return_tensors="pt").to(model.device)
28
+
29
+ output = model.generate(**inputs, max_new_tokens=30)
30
+ st.write(processor.decode(output[0]))
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33