ManishThota commited on
Commit
5cbf359
·
verified ·
1 Parent(s): 962cccf

Update src/text_processor.py

Browse files
Files changed (1) hide show
  1. 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
- model = AutoModelForCausalLM.from_pretrained(
28
- model_path,
29
- device_map=device,
30
- torch_dtype="auto",
31
- trust_remote_code=True,
32
- )
33
- tokenizer = AutoTokenizer.from_pretrained(model_path)
34
- return pipeline("text-generation", model=model, tokenizer=tokenizer)
 
 
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, pipeline) -> 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 = pipeline(messages, **generation_args)
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 process_llm_output(description: str) -> Dict:
82
- generated_logic = generate_logic(description, pipe)
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()