oflakne26 commited on
Commit
0abcce5
1 Parent(s): 57766c5

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +37 -42
main.py CHANGED
@@ -21,8 +21,6 @@ FALLBACK_MODELS = [
21
  "mistralai/Mistral-7B-Instruct-v0.2", "mistralai/Mistral-7B-Instruct-v0.1"
22
  ]
23
 
24
- MAX_RETRIES = 3 # Maximum number of retries
25
-
26
  class InputData(BaseModel):
27
  model: str
28
  system_prompt_template: str
@@ -53,48 +51,45 @@ async def generate_response(data: InputData) -> Any:
53
  seed = random.randint(0, 2**32 - 1)
54
 
55
  models_to_try = [data.model] + FALLBACK_MODELS
56
- retries = 0
57
-
58
- while retries < MAX_RETRIES:
59
- for model in models_to_try:
60
- try:
61
- response = client.text_generation(inputs,
62
- temperature=1.0,
63
- max_new_tokens=1000,
64
- seed=seed)
65
-
66
- strict_response = str(response)
67
-
68
- repaired_response = repair_json(strict_response,
69
- return_objects=True)
70
-
71
- if isinstance(repaired_response, str):
72
- raise HTTPException(status_code=500, detail="Invalid response from model")
73
- else:
74
- cleaned_response = {}
75
- for key, value in repaired_response.items():
76
- cleaned_key = key.replace("###", "")
77
- cleaned_response[cleaned_key] = value
78
-
79
- for i, text in enumerate(cleaned_response["New response"]):
80
- if i <= 2:
81
- sentences = tokenizer.tokenize(text)
82
- if sentences:
83
- cleaned_response["New response"][i] = sentences[0]
84
- else:
85
- del cleaned_response["New response"][i]
86
- if cleaned_response.get("Sentence count"):
87
- if cleaned_response["Sentence count"] > 3:
88
- cleaned_response["Sentence count"] = 3
89
  else:
90
- cleaned_response["Sentence count"] = len(cleaned_response["New response"])
 
 
 
 
 
91
 
92
- data.history += str(cleaned_response)
93
 
94
- return cleaned_response
95
 
96
- except Exception as e:
97
- print(f"Model {model} failed with error: {e}")
98
- retries += 1
99
 
100
- raise HTTPException(status_code=500, detail="All models failed to generate response after maximum retries")
 
21
  "mistralai/Mistral-7B-Instruct-v0.2", "mistralai/Mistral-7B-Instruct-v0.1"
22
  ]
23
 
 
 
24
  class InputData(BaseModel):
25
  model: str
26
  system_prompt_template: str
 
51
  seed = random.randint(0, 2**32 - 1)
52
 
53
  models_to_try = [data.model] + FALLBACK_MODELS
54
+
55
+ for model in models_to_try:
56
+ try:
57
+ response = client.text_generation(inputs,
58
+ temperature=1.0,
59
+ max_new_tokens=1000,
60
+ seed=seed)
61
+
62
+ strict_response = str(response)
63
+
64
+ repaired_response = repair_json(strict_response,
65
+ return_objects=True)
66
+
67
+ if isinstance(repaired_response, str):
68
+ raise HTTPException(status_code=500, detail="Invalid response from model")
69
+ else:
70
+ cleaned_response = {}
71
+ for key, value in repaired_response.items():
72
+ cleaned_key = key.replace("###", "")
73
+ cleaned_response[cleaned_key] = value
74
+
75
+ for i, text in enumerate(cleaned_response["New response"]):
76
+ if i <= 2:
77
+ sentences = tokenizer.tokenize(text)
78
+ if sentences:
79
+ cleaned_response["New response"][i] = sentences[0]
 
 
 
 
 
 
 
80
  else:
81
+ del cleaned_response["New response"][i]
82
+ if cleaned_response.get("Sentence count"):
83
+ if cleaned_response["Sentence count"] > 3:
84
+ cleaned_response["Sentence count"] = 3
85
+ else:
86
+ cleaned_response["Sentence count"] = len(cleaned_response["New response"])
87
 
88
+ data.history += str(cleaned_response)
89
 
90
+ return cleaned_response
91
 
92
+ except Exception as e:
93
+ print(f"Model {model} failed with error: {e}")
 
94
 
95
+ raise HTTPException(status_code=500, detail="All models failed to generate response")