davnas commited on
Commit
d58a9a4
·
verified ·
1 Parent(s): 5b94e9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -37
app.py CHANGED
@@ -1,55 +1,59 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
3
 
4
- # Initialize the client
5
- client = InferenceClient(
6
- model="davnas/Italian_Cousine_2.1",
7
- headers={"Content-Type": "application/json"}
 
 
 
 
8
  )
9
 
10
  def respond(message, history, system_message, max_tokens, temperature, top_p):
11
- # Format the prompt including history and system message
12
- prompt = ""
13
 
14
- # Add system message if provided
15
- if system_message:
16
- prompt += f"{system_message}\n"
17
-
18
- # Add conversation history
19
- for msg in history:
20
- if isinstance(msg, list) and len(msg) == 2:
21
- prompt += f"User: {msg[0]}\nAssistant: {msg[1]}\n"
22
 
23
  # Add current message
24
- prompt += f"User: {message}\nAssistant:"
 
 
 
 
 
 
 
 
25
 
26
- # Prepare parameters for text generation
27
- parameters = {
28
- "max_new_tokens": max_tokens,
29
- "temperature": temperature,
30
- "top_p": top_p,
31
- "return_full_text": False
32
- }
 
 
 
 
33
 
34
- response = ""
35
- try:
36
- # Use generate_text with proper parameters
37
- for token in client.text_generation(
38
- prompt,
39
- stream=True,
40
- **parameters
41
- ):
42
- response += token
43
- yield response
44
- except Exception as e:
45
- yield f"Error: {str(e)}"
46
 
47
  # Create the interface
48
  demo = gr.ChatInterface(
49
  respond,
50
  additional_inputs=[
51
  gr.Textbox(
52
- value="You are a helpful assistant knowledgeable about Italian cuisine.",
53
  label="System message"
54
  ),
55
  gr.Slider(
@@ -73,7 +77,9 @@ demo = gr.ChatInterface(
73
  step=0.05,
74
  label="Top-p (nucleus sampling)"
75
  ),
76
- ]
 
 
77
  )
78
 
79
  if __name__ == "__main__":
 
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__":