davnas commited on
Commit
8c45748
·
verified ·
1 Parent(s): c0b7ba2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -27
app.py CHANGED
@@ -1,42 +1,59 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
3
 
4
- # Initialize the client
5
- client = InferenceClient("davnas/Italian_Cousine_2.1")
 
 
 
 
 
 
 
6
 
7
- def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
8
- # Format the prompt including history and system message
9
- prompt = ""
10
-
11
- # Add system message if provided
12
- if system_message:
13
- prompt = f"{system_message}\n"
14
 
15
- # Add conversation history
16
  for user_msg, assistant_msg in history:
17
- prompt += f"User: {user_msg}\nAssistant: {assistant_msg}\n"
 
18
 
19
  # Add current message
20
- prompt += f"User: {message}\nAssistant:"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- response = ""
23
- # Stream the response
24
- for token in client.text_generation(
25
- prompt,
26
- max_new_tokens=max_tokens,
27
- stream=True,
28
- temperature=temperature,
29
- top_p=top_p,
30
- ):
31
- response += token
32
- yield response
33
 
34
  # Create the interface
35
  demo = gr.ChatInterface(
36
  respond,
37
  additional_inputs=[
38
  gr.Textbox(
39
- value="You are a friendly Chatbot.",
40
  label="System message"
41
  ),
42
  gr.Slider(
@@ -60,8 +77,10 @@ demo = gr.ChatInterface(
60
  step=0.05,
61
  label="Top-p (nucleus sampling)"
62
  ),
63
- ]
 
 
64
  )
65
 
66
  if __name__ == "__main__":
67
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
 
5
+ # Load model and tokenizer
6
+ model_name = "davnas/Italian_Cousine_2.1"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ model_name,
10
+ torch_dtype=torch.float32, # Use float32 for CPU
11
+ low_cpu_mem_usage=True,
12
+ device_map="auto"
13
+ )
14
 
15
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
16
+ # Format the conversation
17
+ messages = [{"role": "system", "content": system_message}]
 
 
 
 
18
 
19
+ # Add history
20
  for user_msg, assistant_msg in history:
21
+ messages.append({"role": "user", "content": user_msg})
22
+ messages.append({"role": "assistant", "content": assistant_msg})
23
 
24
  # Add current message
25
+ messages.append({"role": "user", "content": message})
26
+
27
+ # Create the prompt using the tokenizer's chat template
28
+ input_ids = tokenizer.apply_chat_template(
29
+ messages,
30
+ tokenize=True,
31
+ add_generation_prompt=True,
32
+ return_tensors="pt"
33
+ )
34
+
35
+ # Generate response
36
+ with torch.no_grad():
37
+ output_ids = model.generate(
38
+ input_ids,
39
+ max_new_tokens=max_tokens,
40
+ do_sample=True,
41
+ temperature=temperature,
42
+ top_p=top_p,
43
+ pad_token_id=tokenizer.pad_token_id,
44
+ streaming=True
45
+ )
46
 
47
+ # Decode and return the response
48
+ response = tokenizer.decode(output_ids[0][len(input_ids[0]):], skip_special_tokens=True)
49
+ return response
 
 
 
 
 
 
 
 
50
 
51
  # Create the interface
52
  demo = gr.ChatInterface(
53
  respond,
54
  additional_inputs=[
55
  gr.Textbox(
56
+ value="You are a professional chef assistant who provides accurate and detailed recipes.",
57
  label="System message"
58
  ),
59
  gr.Slider(
 
77
  step=0.05,
78
  label="Top-p (nucleus sampling)"
79
  ),
80
+ ],
81
+ title="Italian Cuisine Chatbot",
82
+ description="Ask me anything about Italian cuisine or cooking!"
83
  )
84
 
85
  if __name__ == "__main__":
86
+ demo.launch(server_name="0.0.0.0", server_port=7860)