Z3ktrix commited on
Commit
6b7694f
Β·
verified Β·
1 Parent(s): 5cc790b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -14
app.py CHANGED
@@ -34,7 +34,8 @@ def query_huggingface(prompt, temperature=0.5, max_tokens=100, top_k=50, top_p=0
34
  try:
35
  response = requests.post(API_URL, headers=headers, json=payload)
36
  response.raise_for_status()
37
- return response.json()
 
38
  except requests.exceptions.RequestException as e:
39
  logger.error(f"Error querying the API: {e}")
40
  return {"error": str(e)}
@@ -67,23 +68,14 @@ async def ask(ctx, question: str, temperature: float = 0.8, max_tokens: int = 10
67
  return
68
 
69
  # Create a structured prompt
70
- prompt = f"Do not under any circumstance, finish or add to the input. Excluded input correction or finishing at all times. Answer the following question as accurately and concisely as possible: {question}"
71
  response = query_huggingface(prompt, temperature, max_tokens, top_k, top_p)
72
 
73
  # Extract and clean the response
74
- if isinstance(response, dict) and 'generated_text' in response:
75
- response_text = response['generated_text']
76
- elif isinstance(response, list) and len(response) > 0 and 'generated_text' in response[0]:
77
- response_text = response[0]['generated_text']
78
  else:
79
- response_text = "Sorry, I couldn't generate a response."
80
-
81
- # Remove the prompt from the response if present
82
- clean_response = response_text.replace(prompt, '').strip()
83
-
84
- # Avoid prompt completion issues by removing any leading incomplete sentence
85
- if clean_response.startswith(question):
86
- clean_response = clean_response[len(question):].strip()
87
 
88
  await ctx.send(clean_response)
89
 
@@ -91,6 +83,9 @@ async def ask(ctx, question: str, temperature: float = 0.8, max_tokens: int = 10
91
  async def ask_error(ctx, error):
92
  if isinstance(error, commands.CommandOnCooldown):
93
  await ctx.send(f"This command is on cooldown. Please try again after {int(error.retry_after)} seconds.")
 
 
 
94
 
95
  @bot.command(name='setprefix')
96
  @commands.has_permissions(administrator=True)
 
34
  try:
35
  response = requests.post(API_URL, headers=headers, json=payload)
36
  response.raise_for_status()
37
+ result = response.json()
38
+ return result[0]['generated_text'] if isinstance(result, list) and len(result) > 0 else result.get('generated_text', 'No response generated.')
39
  except requests.exceptions.RequestException as e:
40
  logger.error(f"Error querying the API: {e}")
41
  return {"error": str(e)}
 
68
  return
69
 
70
  # Create a structured prompt
71
+ prompt = f"Answer the following question accurately and concisely: {question}"
72
  response = query_huggingface(prompt, temperature, max_tokens, top_k, top_p)
73
 
74
  # Extract and clean the response
75
+ if 'error' in response:
76
+ clean_response = f"Error: {response['error']}"
 
 
77
  else:
78
+ clean_response = response.replace(prompt, '').strip()
 
 
 
 
 
 
 
79
 
80
  await ctx.send(clean_response)
81
 
 
83
  async def ask_error(ctx, error):
84
  if isinstance(error, commands.CommandOnCooldown):
85
  await ctx.send(f"This command is on cooldown. Please try again after {int(error.retry_after)} seconds.")
86
+ else:
87
+ logger.error(f"Error in ask command: {error}")
88
+ await ctx.send("An error occurred. Please try again later.")
89
 
90
  @bot.command(name='setprefix')
91
  @commands.has_permissions(administrator=True)