gampala1234 commited on
Commit
c1bf829
Β·
verified Β·
1 Parent(s): 0e9684c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -16
app.py CHANGED
@@ -18,20 +18,31 @@ def load_model():
18
  device_map="auto"
19
  )
20
 
 
 
 
 
 
 
 
21
  def chat(message, history):
22
  # Lazy load model when first used
23
  load_model()
24
 
25
- # Format chat history for Qwen model
26
- formatted_history = "\n".join(
27
- [f"User: {user}\nAssistant: {assistant}" for user, assistant in history]
28
- )
29
- full_prompt = f"{formatted_history}\nUser: {message}\nAssistant:"
 
 
30
 
31
- # Tokenize and generate
32
- inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
 
33
  outputs = model.generate(
34
- **inputs,
 
35
  max_new_tokens=150,
36
  do_sample=True,
37
  top_p=0.9,
@@ -40,20 +51,21 @@ def chat(message, history):
40
  )
41
 
42
  # Decode and clean response
43
- response = tokenizer.decode(outputs[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True)
44
- return response.strip()
 
 
 
 
45
 
46
- # Create interface with Space-optimized settings
47
  with gr.Blocks(title="Maya AI (Lite)", theme=gr.themes.Soft()) as demo:
48
  gr.ChatInterface(
49
  fn=chat,
50
  chatbot=gr.Chatbot(
51
  height=500,
52
  show_copy_button=True,
53
- avatar_images=(
54
- None, # Default user avatar
55
- None # Default bot avatar
56
- )
57
  ),
58
  examples=[
59
  ["Hello! How are you?"],
@@ -64,4 +76,4 @@ with gr.Blocks(title="Maya AI (Lite)", theme=gr.themes.Soft()) as demo:
64
 
65
  # For Hugging Face Spaces
66
  if __name__ == "__main__":
67
- demo.launch()
 
18
  device_map="auto"
19
  )
20
 
21
+ def format_chat_history(history):
22
+ formatted = []
23
+ for human, assistant in history:
24
+ formatted.append({"role": "user", "content": human})
25
+ formatted.append({"role": "assistant", "content": assistant})
26
+ return formatted
27
+
28
  def chat(message, history):
29
  # Lazy load model when first used
30
  load_model()
31
 
32
+ # Format chat history for model input
33
+ messages = format_chat_history(history)
34
+ messages.append({"role": "user", "content": message})
35
+
36
+ # Prepare input for Qwen model
37
+ input_text = "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages])
38
+ input_text += "\nassistant:"
39
 
40
+ inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
41
+
42
+ # Generate response
43
  outputs = model.generate(
44
+ inputs.input_ids,
45
+ attention_mask=inputs.attention_mask,
46
  max_new_tokens=150,
47
  do_sample=True,
48
  top_p=0.9,
 
51
  )
52
 
53
  # Decode and clean response
54
+ response = tokenizer.decode(
55
+ outputs[0][inputs.input_ids.shape[-1]:],
56
+ skip_special_tokens=True
57
+ ).strip()
58
+
59
+ return response
60
 
61
+ # Create interface with proper message format
62
  with gr.Blocks(title="Maya AI (Lite)", theme=gr.themes.Soft()) as demo:
63
  gr.ChatInterface(
64
  fn=chat,
65
  chatbot=gr.Chatbot(
66
  height=500,
67
  show_copy_button=True,
68
+ avatar_images=(None, None), # Default avatars
 
 
 
69
  ),
70
  examples=[
71
  ["Hello! How are you?"],
 
76
 
77
  # For Hugging Face Spaces
78
  if __name__ == "__main__":
79
+ demo.launch(server_name="0.0.0.0", server_port=7860)