lydiasolomon commited on
Commit
95d4ec9
·
verified ·
1 Parent(s): 3c9086c

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +13 -8
main.py CHANGED
@@ -122,20 +122,25 @@ def check_auth(authorization: str | None):
122
  def run_chain(chain, input_dict: dict):
123
  """
124
  Safely run a LangChain PromptTemplate | HuggingFaceEndpoint chain.
125
- Converts output to string and captures errors.
126
  """
127
  try:
128
  # Render template
129
- if hasattr(chain, "prompt"):
130
- prompt_text = chain.prompt.format(**input_dict)
131
- else:
132
- prompt_text = str(input_dict)
133
 
134
- # Generate using HuggingFaceEndpoint (expects str input)
135
  output = chain.llm.generate([{"role": "user", "content": prompt_text}])
136
- return output.generations[0][0].text.strip()
 
 
 
 
 
 
 
 
137
  except Exception:
138
- return {"success": False, "error": "⚠️ LLM error", "details": traceback.format_exc()}
139
 
140
  async def process_audio(file: UploadFile, lang_hint: str | None = None):
141
  suffix = os.path.splitext(file.filename)[1] or ".wav"
 
122
  def run_chain(chain, input_dict: dict):
123
  """
124
  Safely run a LangChain PromptTemplate | HuggingFaceEndpoint chain.
125
+ Returns non-empty string, or detailed error info for debugging.
126
  """
127
  try:
128
  # Render template
129
+ prompt_text = chain.prompt.format(**input_dict) if hasattr(chain, "prompt") else str(input_dict)
 
 
 
130
 
131
+ # Generate using HuggingFaceEndpoint
132
  output = chain.llm.generate([{"role": "user", "content": prompt_text}])
133
+
134
+ # Get text safely
135
+ text = getattr(output.generations[0][0], "text", "") or ""
136
+ text = text.strip()
137
+
138
+ if not text:
139
+ return {"success": False, "error": "⚠️ LLM returned empty output", "prompt": prompt_text}
140
+
141
+ return text
142
  except Exception:
143
+ return {"success": False, "error": "⚠️ LLM error", "details": traceback.format_exc(), "prompt": prompt_text}
144
 
145
  async def process_audio(file: UploadFile, lang_hint: str | None = None):
146
  suffix = os.path.splitext(file.filename)[1] or ".wav"