iqrabatool commited on
Commit
d3f63fd
·
verified ·
1 Parent(s): 85d6742

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -36
app.py CHANGED
@@ -1,48 +1,36 @@
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
 
4
- # Load your model and tokenizer from Hugging Face
5
  model_name = "iqrabatool/finetuned_LLaMA"
6
- model = AutoModelForCausalLM.from_pretrained(model_name)
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
 
9
- def respond(
10
- message,
11
- history: list[tuple[str, str]],
12
- system_message,
13
- max_tokens,
14
- temperature,
15
- top_p,
16
- ):
17
- messages = [{"role": "system", "content": system_message}]
18
-
19
- for val in history:
20
- if val[0]:
21
- messages.append({"role": "user", "content": val[0]})
22
- if val[1]:
23
- messages.append({"role": "assistant", "content": val[1]})
24
-
25
- messages.append({"role": "user", "content": message})
26
-
27
- inputs = tokenizer(message, return_tensors="pt")
28
- outputs = model.generate(**inputs, max_length=max_tokens, temperature=temperature, top_p=top_p)
29
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
30
  return response
31
 
32
- demo = gr.ChatInterface(
33
- respond,
34
- additional_inputs=[
35
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
36
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
37
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
38
- gr.Slider(
39
- minimum=0.1,
40
- maximum=1.0,
41
- value=0.95,
42
- step=0.05,
43
- label="Top-p (nucleus sampling)",
44
- ),
45
- ],
 
 
 
 
46
  )
47
 
48
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
 
4
+ # Load model and tokenizer from Hugging Face
5
  model_name = "iqrabatool/finetuned_LLaMA"
6
+ model = AutoModelForCausalLM.from_pretrained(model_name, use_auth_token=True)
7
  tokenizer = AutoTokenizer.from_pretrained(model_name)
8
 
9
+ def respond(message, system_message, max_tokens, temperature, top_p):
10
+ # Generate response
11
+ inputs = tokenizer(message, return_tensors="pt", max_length=max_tokens, truncation=True, padding=True)
12
+ outputs = model.generate(**inputs, temperature=temperature, top_p=top_p)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
14
  return response
15
 
16
+ # Define interface components
17
+ additional_inputs = [
18
+ gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
19
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
20
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
21
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
22
+ ]
23
+
24
+ # Create the ChatInterface
25
+ demo = gr.Interface(
26
+ fn=respond,
27
+ inputs=["text", "text", "number", "number", "number"],
28
+ outputs="text",
29
+ title="Health Bot",
30
+ description="A chatbot for health-related inquiries.",
31
+ article="The Health Bot assists users with health-related questions and provides information based on a pre-trained language model.",
32
+ examples=[["What are the symptoms of COVID-19?", "Health Bot: COVID-19 symptoms include..."]],
33
+ additional_inputs=additional_inputs
34
  )
35
 
36
  if __name__ == "__main__":