Spaces:
Runtime error
Runtime error
Update src/text_processor.py
Browse files- src/text_processor.py +17 -13
src/text_processor.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import torch
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
3 |
import json
|
@@ -22,22 +23,25 @@ generation_args = {
|
|
22 |
"do_sample": True
|
23 |
}
|
24 |
|
25 |
-
# Load the model and pipeline once
|
26 |
def load_model_pipeline(model_path: str):
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
35 |
|
|
|
36 |
pipe = load_model_pipeline(model_path)
|
37 |
|
38 |
# Generate logic from LLM output
|
39 |
@spaces.GPU(duration=50)
|
40 |
-
def generate_logic(llm_output: str
|
41 |
prompt = f"""
|
42 |
Provide the response in json string for the below keys and context based on the description: '{llm_output}'.
|
43 |
|
@@ -52,7 +56,7 @@ def generate_logic(llm_output: str, pipeline) -> str:
|
|
52 |
{"role": "user", "content": prompt},
|
53 |
]
|
54 |
|
55 |
-
response =
|
56 |
generated_text = response[0]['generated_text']
|
57 |
|
58 |
# Extract JSON from the generated text
|
@@ -78,7 +82,7 @@ class VideoAnalysis(BaseModel):
|
|
78 |
)
|
79 |
|
80 |
# Main function to process LLM output
|
81 |
-
def
|
82 |
-
generated_logic = generate_logic(description
|
83 |
structured_output = VideoAnalysis.from_llm_output(generated_logic)
|
84 |
return structured_output.dict()
|
|
|
1 |
+
|
2 |
import torch
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
4 |
import json
|
|
|
23 |
"do_sample": True
|
24 |
}
|
25 |
|
26 |
+
# Load the model and pipeline once and keep it in memory
|
27 |
def load_model_pipeline(model_path: str):
|
28 |
+
if not hasattr(load_model_pipeline, "pipe"):
|
29 |
+
model = AutoModelForCausalLM.from_pretrained(
|
30 |
+
model_path,
|
31 |
+
device_map=device,
|
32 |
+
torch_dtype="auto",
|
33 |
+
trust_remote_code=True,
|
34 |
+
)
|
35 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
36 |
+
load_model_pipeline.pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
37 |
+
return load_model_pipeline.pipe
|
38 |
|
39 |
+
# Initialize the pipeline and keep it in memory
|
40 |
pipe = load_model_pipeline(model_path)
|
41 |
|
42 |
# Generate logic from LLM output
|
43 |
@spaces.GPU(duration=50)
|
44 |
+
def generate_logic(llm_output: str) -> str:
|
45 |
prompt = f"""
|
46 |
Provide the response in json string for the below keys and context based on the description: '{llm_output}'.
|
47 |
|
|
|
56 |
{"role": "user", "content": prompt},
|
57 |
]
|
58 |
|
59 |
+
response = pipe(messages, **generation_args)
|
60 |
generated_text = response[0]['generated_text']
|
61 |
|
62 |
# Extract JSON from the generated text
|
|
|
82 |
)
|
83 |
|
84 |
# Main function to process LLM output
|
85 |
+
def process_description(description: str) -> Dict:
|
86 |
+
generated_logic = generate_logic(description)
|
87 |
structured_output = VideoAnalysis.from_llm_output(generated_logic)
|
88 |
return structured_output.dict()
|