Bhaskar2611 commited on
Commit
ed84601
·
verified ·
1 Parent(s): 83d4a75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +146 -93
app.py CHANGED
@@ -1,96 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- # Initialize the client with your desired model
5
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
-
7
- # Define the system prompt as an AI Dermatologist
8
- def format_prompt(message, history):
9
- prompt = "<s>"
10
- # Start the conversation with a system message
11
- prompt += "[INST] You are an AI Dermatologist chatbot designed to assist users with only hair care by only providing text and if user information is not provided related to hair then ask what they want to know related to hair.[/INST]"
12
- for user_prompt, bot_response in history:
13
- prompt += f"[INST] {user_prompt} [/INST]"
14
- prompt += f" {bot_response}</s> "
15
- prompt += f"[INST] {message} [/INST]"
16
- return prompt
17
-
18
- # Function to generate responses with the AI Dermatologist context
19
- def generate(
20
- prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0
21
- ):
22
- temperature = float(temperature)
23
- if temperature < 1e-2:
24
- temperature = 1e-2
25
- top_p = float(top_p)
26
-
27
- generate_kwargs = dict(
28
- temperature=temperature,
29
- max_new_tokens=max_new_tokens,
30
- top_p=top_p,
31
- repetition_penalty=repetition_penalty,
32
- do_sample=True,
33
- seed=42,
34
- )
35
-
36
- formatted_prompt = format_prompt(prompt, history)
37
-
38
- stream = client.text_generation(
39
- formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False
40
- )
41
- output = ""
42
-
43
- for response in stream:
44
- output += response.token.text
45
- yield output
46
- return output
47
-
48
- # Customizable input controls for the chatbot interface
49
- Settings = [
50
- gr.Slider(
51
- label="Temperature",
52
- value=0.9,
53
- minimum=0.0,
54
- maximum=1.0,
55
- step=0.05,
56
- interactive=True,
57
- info="Higher values produce more diverse outputs",
58
- ),
59
- gr.Slider(
60
- label="Max new tokens",
61
- value=256,
62
- minimum=0,
63
- maximum=1048,
64
- step=64,
65
- interactive=True,
66
- info="The maximum numbers of new tokens",
67
- ),
68
- gr.Slider(
69
- label="Top-p (nucleus sampling)",
70
- value=0.90,
71
- minimum=0.0,
72
- maximum=1,
73
- step=0.05,
74
- interactive=True,
75
- info="Higher values sample more low-probability tokens",
76
- ),
77
- gr.Slider(
78
- label="Repetition penalty",
79
- value=1.2,
80
- minimum=1.0,
81
- maximum=2.0,
82
- step=0.05,
83
- interactive=True,
84
- info="Penalize repeated tokens",
85
- )
86
- ]
87
- # Define the chatbot interface with the starting system message as AI Dermatologist
88
- gr.ChatInterface(
89
- fn=generate,
90
- chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, layout="panel"),
91
- additional_inputs = Settings,
92
- title="Hair bot"
93
- ).launch(show_api=False)
94
-
95
- # Load your model after launching the interface
96
- gr.load("models/Bhaskar2611/Capstone").launch()
 
1
+ # import gradio as gr
2
+ # from huggingface_hub import InferenceClient
3
+
4
+ # # Initialize the client with your desired model
5
+ # client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
6
+
7
+ # # Define the system prompt as an AI Dermatologist
8
+ # def format_prompt(message, history):
9
+ # prompt = "<s>"
10
+ # # Start the conversation with a system message
11
+ # prompt += "[INST] You are an AI Dermatologist chatbot designed to assist users with only hair care by only providing text and if user information is not provided related to hair then ask what they want to know related to hair.[/INST]"
12
+ # for user_prompt, bot_response in history:
13
+ # prompt += f"[INST] {user_prompt} [/INST]"
14
+ # prompt += f" {bot_response}</s> "
15
+ # prompt += f"[INST] {message} [/INST]"
16
+ # return prompt
17
+
18
+ # # Function to generate responses with the AI Dermatologist context
19
+ # def generate(
20
+ # prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0
21
+ # ):
22
+ # temperature = float(temperature)
23
+ # if temperature < 1e-2:
24
+ # temperature = 1e-2
25
+ # top_p = float(top_p)
26
+
27
+ # generate_kwargs = dict(
28
+ # temperature=temperature,
29
+ # max_new_tokens=max_new_tokens,
30
+ # top_p=top_p,
31
+ # repetition_penalty=repetition_penalty,
32
+ # do_sample=True,
33
+ # seed=42,
34
+ # )
35
+
36
+ # formatted_prompt = format_prompt(prompt, history)
37
+
38
+ # stream = client.text_generation(
39
+ # formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False
40
+ # )
41
+ # output = ""
42
+
43
+ # for response in stream:
44
+ # output += response.token.text
45
+ # yield output
46
+ # return output
47
+
48
+ # # Customizable input controls for the chatbot interface
49
+ # Settings = [
50
+ # gr.Slider(
51
+ # label="Temperature",
52
+ # value=0.9,
53
+ # minimum=0.0,
54
+ # maximum=1.0,
55
+ # step=0.05,
56
+ # interactive=True,
57
+ # info="Higher values produce more diverse outputs",
58
+ # ),
59
+ # gr.Slider(
60
+ # label="Max new tokens",
61
+ # value=256,
62
+ # minimum=0,
63
+ # maximum=1048,
64
+ # step=64,
65
+ # interactive=True,
66
+ # info="The maximum numbers of new tokens",
67
+ # ),
68
+ # gr.Slider(
69
+ # label="Top-p (nucleus sampling)",
70
+ # value=0.90,
71
+ # minimum=0.0,
72
+ # maximum=1,
73
+ # step=0.05,
74
+ # interactive=True,
75
+ # info="Higher values sample more low-probability tokens",
76
+ # ),
77
+ # gr.Slider(
78
+ # label="Repetition penalty",
79
+ # value=1.2,
80
+ # minimum=1.0,
81
+ # maximum=2.0,
82
+ # step=0.05,
83
+ # interactive=True,
84
+ # info="Penalize repeated tokens",
85
+ # )
86
+ # ]
87
+ # # Define the chatbot interface with the starting system message as AI Dermatologist
88
+ # gr.ChatInterface(
89
+ # fn=generate,
90
+ # chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, layout="panel"),
91
+ # additional_inputs = Settings,
92
+ # title="Hair bot"
93
+ # ).launch(show_api=False)
94
+
95
+ # # Load your model after launching the interface
96
+ # gr.load("models/Bhaskar2611/Capstone").launch()
97
+
98
+ import os
99
  import gradio as gr
100
  from huggingface_hub import InferenceClient
101
+ from dotenv import load_dotenv
102
+
103
+ # Load API token from .env or environment
104
+ load_dotenv()
105
+ HF_TOKEN = os.getenv("HF_TOKEN") # or directly use your token here
106
+
107
+ # Initialize the Hugging Face inference client
108
+ client = InferenceClient(
109
+ model="mistralai/Mistral-7B-Instruct-v0.3",
110
+ token=HF_TOKEN
111
+ )
112
+
113
+ # Skin assistant prompt
114
+ HAIR_ASSISTANT_PROMPT = (
115
+ "You are an AI Dermatologist chatbot designed to assist users with Hair by only providing text "
116
+ "and if user information is not provided related to Hair then ask what they want to know related to Hair."
117
+ )
118
+
119
+ def respond(message, history):
120
+ messages = [{"role": "system", "content": HAIR_ASSISTANT_PROMPT}]
121
+ for user_msg, bot_msg in history:
122
+ if user_msg:
123
+ messages.append({"role": "user", "content": user_msg})
124
+ if bot_msg:
125
+ messages.append({"role": "assistant", "content": bot_msg})
126
+ messages.append({"role": "user", "content": message})
127
+
128
+ response = ""
129
+ for chunk in client.chat.completions.create(
130
+ model="mistralai/Mistral-7B-Instruct-v0.3",
131
+ messages=messages,
132
+ max_tokens=1024,
133
+ temperature=0.7,
134
+ top_p=0.95,
135
+ stream=True,
136
+ ):
137
+ token = chunk.choices[0].delta.get("content", "")
138
+ response += token
139
+ yield response
140
+
141
+ # Launch Gradio interface
142
+ demo = gr.ChatInterface(
143
+ fn=respond,
144
+ title="Hair-Bot",
145
+ theme="default"
146
+ )
147
 
148
+ if __name__ == "__main__":
149
+ demo.launch()