Simon0900 commited on
Commit
2d060c2
·
verified ·
1 Parent(s): 6866ef1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -37
app.py CHANGED
@@ -1,71 +1,66 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
 
4
 
5
  def respond(
6
  message,
7
- history: list[dict[str, str]],
8
  system_message,
9
  max_tokens,
10
  temperature,
11
  top_p,
12
- #hf_token: gr.OAuthToken,
13
  ):
14
  """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
  """
17
- #client = InferenceClient(token=hf_token.token, model="Simon0900/personalized-fitness-coach-JSON")
18
- client = InferenceClient(model="Simon0900/personalized-fitness-coach-JSON")
19
-
20
- messages = [{"role": "system", "content": system_message}]
21
 
22
- messages.extend(history)
 
23
 
24
- messages.append({"role": "user", "content": message})
 
25
 
26
- response = ""
27
 
28
- for message in client.chat_completion(
29
- messages,
30
- max_tokens=max_tokens,
31
- stream=True,
32
  temperature=temperature,
33
  top_p=top_p,
34
- ):
35
- choices = message.choices
36
- token = ""
37
- if len(choices) and choices[0].delta.content:
38
- token = choices[0].delta.content
39
 
40
- response += token
41
- yield response
42
 
43
 
44
- """
45
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
46
- """
47
  chatbot = gr.ChatInterface(
48
  respond,
49
  additional_inputs=[
50
- gr.Textbox(value="""You are a fitness expert. You will create a workout plan in JSON-schema, based on the given user description. Strictly use the following format:\n\nJSON-schematic:\n{\n \"goal\": <workout goal>,\n \"intensity\": <intensity level>,\n \"workout_equipment\": <workout equipment>,\n \"workout_days\": <workout days>,\n \"time_per_workout\": <time per workout>,\n \"limitations\": <limitations>,\n \"days\": [\n {\n \"day\": <day of week>,\n \"focus\": <muscle groups>,\n \"estimated_duration_min\": <estimated duration>,\n \"warmup_exercise\": <warmup exercise>,\n \"exercises\": [\n {\n \"name\": <name of exercise>,\n \"muscle_group\": <muscle group>,\n \"instructions\": <instructions>,\n \"sets\": <number of sets>,\n \"reps\": <number of reps>,\n \"rest_seconds\": <rest duration>\n }\n OR\n {\n \"name\": <name of exercise>,\n \"muscle_group\": \"cardio\",\n \"instructions\": <instructions>,\n \"duration_min\": <duration>\n }\n ]\n }\n ]\n}\n\nRules:\n- Warmup must differ from exercises\n- At least 3 exercises per day\n- No repeating exercises on consecutive days\n- Optimize for recovery and variation\n- Spread out the workout days over a week\n- Not every exercise has to use the equipment\n- Cardio exercises do not have to use the equipment\n- Respond only with valid JSON using double quotes\n
51
- """, label="System message"),
52
- gr.Slider(minimum=1, maximum=4096, value=4096, step=1, label="Max new tokens"),
53
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
54
- gr.Slider(
55
- minimum=0.1,
56
- maximum=1.0,
57
- value=0.95,
58
- step=0.05,
59
- label="Top-p (nucleus sampling)",
 
 
60
  ),
 
 
 
61
  ],
62
  )
63
 
 
64
  with gr.Blocks() as demo:
65
- with gr.Sidebar():
66
- gr.LoginButton()
67
  chatbot.render()
68
 
69
 
70
  if __name__ == "__main__":
71
- demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ client = InferenceClient(model="Simon0900/personalized-fitness-coach-JSON")
5
+
6
 
7
  def respond(
8
  message,
9
+ history,
10
  system_message,
11
  max_tokens,
12
  temperature,
13
  top_p,
 
14
  ):
15
  """
16
+ Generate response using text-generation (works for non-chat models)
17
  """
 
 
 
 
18
 
19
+ # Build a simple prompt (chat-style formatting for a base model)
20
+ prompt = system_message.strip() + "\n\n"
21
 
22
+ for user_msg, bot_msg in history:
23
+ prompt += f"User: {user_msg}\nAssistant: {bot_msg}\n"
24
 
25
+ prompt += f"User: {message}\nAssistant:"
26
 
27
+ response = client.text_generation(
28
+ prompt,
29
+ max_new_tokens=max_tokens,
 
30
  temperature=temperature,
31
  top_p=top_p,
32
+ return_full_text=False,
33
+ )
 
 
 
34
 
35
+ return response
 
36
 
37
 
 
 
 
38
  chatbot = gr.ChatInterface(
39
  respond,
40
  additional_inputs=[
41
+ gr.Textbox(
42
+ value="""You are a fitness expert. You will create a workout plan in JSON format.
43
+
44
+ Return ONLY valid JSON using double quotes.
45
+
46
+ Rules:
47
+ - At least 3 exercises per day
48
+ - No repeated exercises on consecutive days
49
+ - Optimize recovery and variation
50
+ - Include warmup separate from exercises
51
+ """,
52
+ label="System message",
53
  ),
54
+ gr.Slider(1, 2048, value=512, step=1, label="Max new tokens"),
55
+ gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature"),
56
+ gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p"),
57
  ],
58
  )
59
 
60
+
61
  with gr.Blocks() as demo:
 
 
62
  chatbot.render()
63
 
64
 
65
  if __name__ == "__main__":
66
+ demo.launch()