benfunke98 commited on
Commit
f8bd053
·
verified ·
1 Parent(s): 3f69fec

edited generate code

Browse files
Files changed (1) hide show
  1. app.py +33 -37
app.py CHANGED
@@ -16,15 +16,6 @@ pipe = pipeline("text2text-generation", model="google/flan-t5-small")
16
  categories = ('Heart', 'Oblong', 'Oval', 'Round', 'Square')
17
  learn = load_learner('model.pkl')
18
 
19
- # Initialize the Code Llama Instruct pipeline (example with 7B model)
20
- llama_model_id = "meta-llama/CodeLlama-7b-Instruct-hf"
21
- llama_pipeline = pipeline(
22
- "text-generation",
23
- model=llama_model_id,
24
- model_kwargs={"torch_dtype": torch.bfloat16},
25
- device_map="auto"
26
- )
27
-
28
  # Überprüfe, ob das Zugriffstoken vorhanden ist
29
  if access_token is None:
30
  raise ValueError("Access token is missing. Make sure it is set as an environment variable.")
@@ -69,36 +60,41 @@ async def face_analyse(file: UploadFile = File(...)):
69
  # Assuming categories is a list of category labels
70
  return dict(zip(categories, map(float, probs)))
71
 
72
- @app.post("/extract-frame-details")
73
- def extract_frame_details(text: str):
 
 
 
 
 
 
 
 
 
 
74
  """
75
- Using the Code Llama Instruct pipeline from `transformers`, extract frame
76
- details from the given input text. The model used is `meta-llama/CodeLlama-7b-Instruct-hf`.
77
  """
78
- messages = [
79
- {"role": "system", "content": "Please provide details about frames in JSON format."},
80
- {"role": "user", "content": text},
81
- ]
 
 
 
 
 
 
 
82
 
83
- terminators = [
84
- llama_pipeline.tokenizer.eos_token_id,
85
- llama_pipeline.tokenizer.convert_tokens_to_ids("")
86
- ]
87
 
88
- outputs = llama_pipeline(
89
- messages,
90
- max_new_tokens=256,
91
- eos_token_id=terminators,
92
- do_sample=True,
93
- temperature=0.6,
94
- top_p=0.9,
95
- )
96
-
97
- generated_text = outputs[0]["generated_text"]
98
-
99
- try:
100
- extracted_info = json.loads(generated_text)
101
- except json.JSONDecodeError:
102
- return {"error": "Failed to parse the generated text as JSON."}
103
 
104
- return extracted_info
 
 
 
 
16
  categories = ('Heart', 'Oblong', 'Oval', 'Round', 'Square')
17
  learn = load_learner('model.pkl')
18
 
 
 
 
 
 
 
 
 
 
19
  # Überprüfe, ob das Zugriffstoken vorhanden ist
20
  if access_token is None:
21
  raise ValueError("Access token is missing. Make sure it is set as an environment variable.")
 
60
  # Assuming categories is a list of category labels
61
  return dict(zip(categories, map(float, probs)))
62
 
63
+ # Initialisiere das Modell und den Tokenizer
64
+ model = "meta-llama/CodeLlama-7b-hf"
65
+ tokenizer = AutoTokenizer.from_pretrained(model)
66
+ llama_pipeline = pipeline(
67
+ "text-generation",
68
+ model=model,
69
+ torch_dtype=torch.float16,
70
+ device_map="auto",
71
+ )
72
+
73
+ @app.get("/generate_json")
74
+ def generate_code(text: str):
75
  """
76
+ Using the Code Llama pipeline from `transformers`, generate code
77
+ from the given input text. The model used is `meta-llama/CodeLlama-7b-hf`.
78
  """
79
+ try:
80
+ sequences = llama_pipeline(
81
+ text,
82
+ do_sample=True,
83
+ top_k=10,
84
+ temperature=0.1,
85
+ top_p=0.95,
86
+ num_return_sequences=1,
87
+ eos_token_id=tokenizer.eos_token_id,
88
+ max_length=200,
89
+ )
90
 
91
+ generated_text = sequences[0]["generated_text"]
92
+ except Exception as e:
93
+ raise HTTPException(status_code=500, detail=str(e))
 
94
 
95
+ return {"generated_text": generated_text}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
+ # Beispielaufruf mit curl:
98
+ # curl -X 'GET' \
99
+ # 'http://localhost:8000/generate_code?text=import%20socket%0A%0Adef%20ping_exponential_backoff(host%3A%20str)%3A' \
100
+ # -H 'accept: application/json'