Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,146 +8,132 @@ from langchain import LLMChain, PromptTemplate
|
|
8 |
from langchain_community.llms import Ollama
|
9 |
from langchain_core.output_parsers import StrOutputParser
|
10 |
|
11 |
-
# Load the
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
17 |
-
model.to(device)
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
)
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
ollama_llm = Ollama(model="llama3.1")
|
141 |
-
output_parser = StrOutputParser()
|
142 |
-
chain = LLMChain(
|
143 |
-
llm=ollama_llm,
|
144 |
-
prompt=prompt_template,
|
145 |
-
output_parser=output_parser
|
146 |
-
)
|
147 |
-
|
148 |
-
# Generate the story using LangChain
|
149 |
-
story = chain.run({"descriptions": combined_text})
|
150 |
-
|
151 |
-
# Display the generated story
|
152 |
-
st.write("Generated Story:")
|
153 |
-
st.write(story)
|
|
|
8 |
from langchain_community.llms import Ollama
|
9 |
from langchain_core.output_parsers import StrOutputParser
|
10 |
|
11 |
+
# Step 1: Load the model
|
12 |
+
def load_model():
|
13 |
+
st.write("Loading the model...")
|
14 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
|
15 |
+
model = Qwen2VLForConditionalGeneration.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
|
16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
17 |
+
model.to(device)
|
18 |
+
st.write("Model loaded successfully!")
|
19 |
+
return processor, model, device
|
20 |
+
|
21 |
+
# Step 2: Upload image or video
|
22 |
+
def upload_media():
|
23 |
+
st.write("Step 2: Upload an image or video")
|
24 |
+
return st.file_uploader("Choose images or videos...", type=["jpg", "jpeg", "png", "mp4", "avi", "mov"], accept_multiple_files=True)
|
25 |
+
|
26 |
+
# Step 3: Enter your question
|
27 |
+
def get_user_question():
|
28 |
+
st.write("Step 3: Enter your question")
|
29 |
+
return st.text_input("Ask a question about the images or videos:")
|
30 |
+
|
31 |
+
# Process image
|
32 |
+
def process_image(uploaded_file):
|
33 |
+
image = Image.open(uploaded_file)
|
34 |
+
image = image.resize((256, 256)) # Reduce size to save memory
|
35 |
+
st.image(image, caption='Uploaded Image.', use_column_width=True)
|
36 |
+
return image
|
37 |
+
|
38 |
+
# Process video
|
39 |
+
def process_video(uploaded_file):
|
40 |
+
tfile = tempfile.NamedTemporaryFile(delete=False)
|
41 |
+
tfile.write(uploaded_file.read())
|
42 |
+
cap = cv2.VideoCapture(tfile.name)
|
43 |
+
ret, frame = cap.read()
|
44 |
+
cap.release()
|
45 |
+
if not ret:
|
46 |
+
st.error("Failed to read the video file.")
|
47 |
+
return None
|
48 |
+
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
49 |
+
image = image.resize((256, 256)) # Reduce size to save memory
|
50 |
+
st.image(image, caption='First Frame of Uploaded Video.', use_column_width=True)
|
51 |
+
return image
|
52 |
+
|
53 |
+
# Generate description
|
54 |
+
def generate_description(processor, model, device, image, user_question):
|
55 |
+
messages = [
|
56 |
+
{
|
57 |
+
"role": "user",
|
58 |
+
"content": [
|
59 |
+
{"type": "image", "image": image},
|
60 |
+
{"type": "text", "text": user_question},
|
61 |
+
],
|
62 |
+
}
|
63 |
+
]
|
64 |
+
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
65 |
+
inputs = processor(text=[text], images=[image], padding=True, return_tensors="pt").to(device)
|
66 |
+
generated_ids = model.generate(**inputs, max_new_tokens=512)
|
67 |
+
generated_ids_trimmed = [out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)]
|
68 |
+
output_text = processor.batch_decode(generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False)
|
69 |
+
return output_text[0]
|
70 |
+
|
71 |
+
# Generate story
|
72 |
+
def generate_story(descriptions):
|
73 |
+
combined_text = " ".join(descriptions)
|
74 |
+
prompt_template = PromptTemplate(
|
75 |
+
input_variables=["descriptions"],
|
76 |
+
template="Based on the following descriptions, create a short story:\n\n{descriptions}\n\nStory:"
|
77 |
+
)
|
78 |
+
ollama_llm = Ollama(model="llama3.1")
|
79 |
+
output_parser = StrOutputParser()
|
80 |
+
chain = LLMChain(llm=ollama_llm, prompt=prompt_template, output_parser=output_parser)
|
81 |
+
return chain.run({"descriptions": combined_text})
|
82 |
+
|
83 |
+
# Main function to control the flow
|
84 |
+
def main():
|
85 |
+
st.title("Media Description Generator")
|
86 |
+
|
87 |
+
# Step 1: Load the model
|
88 |
+
processor, model, device = load_model()
|
89 |
+
|
90 |
+
# Step 2: Upload image or video
|
91 |
+
uploaded_files = upload_media()
|
92 |
+
|
93 |
+
if uploaded_files:
|
94 |
+
# Step 3: Enter your question
|
95 |
+
user_question = get_user_question()
|
96 |
+
|
97 |
+
if user_question:
|
98 |
+
# Step 4: Generate description
|
99 |
+
st.write("Step 4: Generate description")
|
100 |
+
generate_description_button = st.button("Generate Description")
|
101 |
+
|
102 |
+
if generate_description_button:
|
103 |
+
all_output_texts = []
|
104 |
+
|
105 |
+
for uploaded_file in uploaded_files:
|
106 |
+
file_type = uploaded_file.type.split('/')[0]
|
107 |
+
image = None
|
108 |
+
|
109 |
+
if file_type == 'image':
|
110 |
+
image = process_image(uploaded_file)
|
111 |
+
elif file_type == 'video':
|
112 |
+
image = process_video(uploaded_file)
|
113 |
+
else:
|
114 |
+
st.error("Unsupported file type.")
|
115 |
+
continue
|
116 |
+
|
117 |
+
if image:
|
118 |
+
description = generate_description(processor, model, device, image, user_question)
|
119 |
+
st.write("Description:")
|
120 |
+
st.write(description)
|
121 |
+
all_output_texts.append(description)
|
122 |
+
|
123 |
+
# Clear memory after processing each file
|
124 |
+
del image
|
125 |
+
torch.cuda.empty_cache()
|
126 |
+
torch.manual_seed(0)
|
127 |
+
|
128 |
+
if all_output_texts:
|
129 |
+
# Step 5: Generate story
|
130 |
+
st.write("Step 5: Generate story")
|
131 |
+
generate_story_button = st.button("Generate Story")
|
132 |
+
|
133 |
+
if generate_story_button:
|
134 |
+
story = generate_story(all_output_texts)
|
135 |
+
st.write("Generated Story:")
|
136 |
+
st.write(story)
|
137 |
+
|
138 |
+
if __name__ == "__main__":
|
139 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|