Hjgugugjhuhjggg commited on
Commit
50c545e
·
verified ·
1 Parent(s): d2283fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -12
app.py CHANGED
@@ -7,6 +7,7 @@ import os
7
  from dotenv import load_dotenv
8
  from pydantic import BaseModel
9
  import requests
 
10
 
11
  load_dotenv()
12
  HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
@@ -41,6 +42,7 @@ def load_model(model_config):
41
  return model
42
  except Exception as e:
43
  print(f"Error loading model {model_name}: {e}")
 
44
  models[model_name] = None
45
  return None
46
 
@@ -72,21 +74,19 @@ def generate_model_response(model, inputs, max_tokens_per_part):
72
  return []
73
  full_response = ""
74
  responses = []
75
- tokens_generated = 0
76
- while True:
77
- response_part = model(inputs, max_tokens=max_tokens_per_part, stop=["\n\n"])
78
- text = response_part['choices'][0]['text']
79
- if not text.strip():
80
- break
81
- full_response += text
82
- tokens_generated += len(response_part['choices'][0]['token'])
83
- responses.append(remove_duplicates(text))
84
- if "eos_token" in response_part['choices'][0]['token']:
85
- break
86
- inputs = ""
87
  return responses
 
88
  except Exception as e:
89
  print(f"Error generating response: {e}")
 
90
  return [f"Error: {e}"]
91
 
92
  app = FastAPI()
 
7
  from dotenv import load_dotenv
8
  from pydantic import BaseModel
9
  import requests
10
+ import traceback
11
 
12
  load_dotenv()
13
  HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
 
42
  return model
43
  except Exception as e:
44
  print(f"Error loading model {model_name}: {e}")
45
+ traceback.print_exc()
46
  models[model_name] = None
47
  return None
48
 
 
74
  return []
75
  full_response = ""
76
  responses = []
77
+ response = model(inputs, max_tokens=max_tokens_per_part, stop=["\n\n"])
78
+ if 'choices' not in response or len(response['choices']) == 0 or 'text' not in response['choices'][0]:
79
+ return [f"Error: Invalid model response format"]
80
+
81
+ text = response['choices'][0]['text']
82
+ if text:
83
+ responses.append(remove_duplicates(text))
84
+
 
 
 
 
85
  return responses
86
+
87
  except Exception as e:
88
  print(f"Error generating response: {e}")
89
+ traceback.print_exc()
90
  return [f"Error: {e}"]
91
 
92
  app = FastAPI()