Futuresony commited on
Commit
ef1a69d
·
verified ·
1 Parent(s): 5efa1e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -27
app.py CHANGED
@@ -3,41 +3,31 @@ from huggingface_hub import InferenceClient
3
 
4
  client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
5
 
6
- def format_alpaca_prompt(history, user_input, system_prompt):
7
- """Formats input in Alpaca/LLaMA style with history"""
8
- history_prompt = "\n".join([f"User: {h[0]}\nAssistant: {h[1]}" for h in history])
9
  prompt = f"""{system_prompt}
10
 
11
- {history_prompt}
 
12
 
13
- User: {user_input}
14
- Assistant:
15
  """
16
  return prompt
17
 
18
  def respond(message, history, system_message, max_tokens, temperature, top_p):
19
- formatted_prompt = format_alpaca_prompt(history, message, system_message)
20
- cleaned_response = ""
21
 
22
- # Retry mechanism to regenerate response if it is empty
23
- for _ in range(3): # Retry up to 3 times
24
- response = client.text_generation(
25
- formatted_prompt,
26
- max_new_tokens=max_tokens,
27
- temperature=temperature,
28
- top_p=top_p,
29
- )
30
 
31
- # Extract only the response
32
- cleaned_response = response.split("Assistant:")[-1].strip()
33
-
34
- if cleaned_response:
35
- break # Exit loop if a valid response is generated
36
-
37
- if not cleaned_response:
38
- cleaned_response = "I'm sorry, I couldn't generate a response. Please try again."
39
-
40
- yield cleaned_response # Output only the answer
41
 
42
  demo = gr.ChatInterface(
43
  respond,
@@ -50,4 +40,4 @@ demo = gr.ChatInterface(
50
  )
51
 
52
  if __name__ == "__main__":
53
- demo.launch()
 
3
 
4
  client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
5
 
6
+ def format_alpaca_prompt(user_input, system_prompt):
7
+ """Formats input in Alpaca/LLaMA style"""
 
8
  prompt = f"""{system_prompt}
9
 
10
+ ### Instruction:
11
+ {user_input}
12
 
13
+ ### Response:
 
14
  """
15
  return prompt
16
 
17
  def respond(message, history, system_message, max_tokens, temperature, top_p):
18
+ formatted_prompt = format_alpaca_prompt(message, system_message)
 
19
 
20
+ response = client.text_generation(
21
+ formatted_prompt,
22
+ max_new_tokens=max_tokens,
23
+ temperature=temperature,
24
+ top_p=top_p,
25
+ )
 
 
26
 
27
+ # Extract only the response
28
+ cleaned_response = response.split("### Response:")[-1].strip()
29
+
30
+ yield cleaned_response # ✅ Output only the answer
 
 
 
 
 
 
31
 
32
  demo = gr.ChatInterface(
33
  respond,
 
40
  )
41
 
42
  if __name__ == "__main__":
43
+ demo.launch()