Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,20 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
|
3 |
from PIL import Image
|
4 |
import torch
|
5 |
import cv2
|
6 |
import tempfile
|
|
|
|
|
|
|
7 |
|
8 |
# Load the processor and model directly
|
9 |
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
|
10 |
model = Qwen2VLForConditionalGeneration.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
|
11 |
|
12 |
-
# Load Meta-Llama model and tokenizer for story generation
|
13 |
-
llama_tokenizer = AutoTokenizer.from_pretrained("meta-llama/Meta-Llama-3.1-8B-Instruct")
|
14 |
-
llama_model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3.1-8B-Instruct")
|
15 |
-
|
16 |
# Check if CUDA is available and set the device accordingly
|
17 |
-
device = torch.device("cpu")
|
18 |
model.to(device)
|
19 |
-
llama_model.to(device)
|
20 |
|
21 |
# Streamlit app
|
22 |
st.title("Media Description Generator")
|
@@ -96,7 +94,7 @@ if uploaded_files:
|
|
96 |
inputs = inputs.to(device) # Ensure inputs are on the same device as the model
|
97 |
|
98 |
# Inference: Generation of the output
|
99 |
-
generated_ids = model.generate(**inputs, max_new_tokens=
|
100 |
generated_ids_trimmed = [
|
101 |
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
102 |
]
|
@@ -121,10 +119,23 @@ if uploaded_files:
|
|
121 |
# Create a custom prompt
|
122 |
custom_prompt = f"Based on the following descriptions, create a short story:\n\n{combined_text}\n\nStory:"
|
123 |
|
124 |
-
#
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
# Display the generated story
|
130 |
st.write("Generated Story:")
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
|
3 |
from PIL import Image
|
4 |
import torch
|
5 |
import cv2
|
6 |
import tempfile
|
7 |
+
from langchain import LLMChain, PromptTemplate
|
8 |
+
from langchain_community.llms import Ollama
|
9 |
+
from langchain_core.output_parsers import StrOutputParser
|
10 |
|
11 |
# Load the processor and model directly
|
12 |
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
|
13 |
model = Qwen2VLForConditionalGeneration.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
|
14 |
|
|
|
|
|
|
|
|
|
15 |
# Check if CUDA is available and set the device accordingly
|
16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
17 |
model.to(device)
|
|
|
18 |
|
19 |
# Streamlit app
|
20 |
st.title("Media Description Generator")
|
|
|
94 |
inputs = inputs.to(device) # Ensure inputs are on the same device as the model
|
95 |
|
96 |
# Inference: Generation of the output
|
97 |
+
generated_ids = model.generate(**inputs, max_new_tokens=512)
|
98 |
generated_ids_trimmed = [
|
99 |
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
100 |
]
|
|
|
119 |
# Create a custom prompt
|
120 |
custom_prompt = f"Based on the following descriptions, create a short story:\n\n{combined_text}\n\nStory:"
|
121 |
|
122 |
+
# Define the prompt template for LangChain
|
123 |
+
prompt_template = PromptTemplate(
|
124 |
+
input_variables=["descriptions"],
|
125 |
+
template="Based on the following descriptions, create a short story:\n\n{descriptions}\n\nStory:"
|
126 |
+
)
|
127 |
+
|
128 |
+
# Create the LLMChain with the Ollama model
|
129 |
+
ollama_llm = Ollama(model="llama3.1")
|
130 |
+
output_parser = StrOutputParser()
|
131 |
+
chain = LLMChain(
|
132 |
+
llm=ollama_llm,
|
133 |
+
prompt=prompt_template,
|
134 |
+
output_parser=output_parser
|
135 |
+
)
|
136 |
+
|
137 |
+
# Generate the story using LangChain
|
138 |
+
story = chain.run({"descriptions": combined_text})
|
139 |
|
140 |
# Display the generated story
|
141 |
st.write("Generated Story:")
|