Wenye He commited on
Commit
c31bf37
·
verified ·
1 Parent(s): 97128d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -2
app.py CHANGED
@@ -47,6 +47,53 @@ class ChatModel:
47
  self.models[model_name] = model
48
  self.tokenizers[model_name] = tokenizer
49
 
50
- # ... (keep the rest of the code the same as previous version)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- # ... (remaining code identical to previous implementation)
 
47
  self.models[model_name] = model
48
  self.tokenizers[model_name] = tokenizer
49
 
50
+ def generate(self, message, model_name, history):
51
+ self.load_model(model_name)
52
+ config = MODEL_CONFIG[model_name]
53
+
54
+ # Format prompt
55
+ prompt = config["template"].format(message=message)
56
+
57
+ # Create pipeline
58
+ pipe = pipeline(
59
+ "text-generation",
60
+ model=self.models[model_name],
61
+ tokenizer=self.tokenizers[model_name],
62
+ max_new_tokens=384,
63
+ temperature=0.7,
64
+ top_p=0.9,
65
+ repetition_penalty=1.1,
66
+ do_sample=True,
67
+ return_full_text=False
68
+ )
69
+
70
+ response = pipe(prompt)[0]['generated_text']
71
+ return response.strip()
72
+
73
+ model_handler = ChatModel()
74
+
75
+ def chat(message, history, model_choice):
76
+ try:
77
+ response = model_handler.generate(message, model_choice, history)
78
+ return [(message, response)]
79
+ except Exception as e:
80
+ return [(message, f"Error: {str(e)}")]
81
+
82
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
83
+ gr.Markdown("# 🚀 Phi-3 vs Llama-3 Chatbot")
84
+ with gr.Row():
85
+ model_choice = gr.Dropdown(
86
+ choices=["phi-3", "llama3-8b"],
87
+ label="Select Model",
88
+ value="phi-3"
89
+ )
90
+ chatbot = gr.Chatbot(height=400)
91
+ msg = gr.Textbox(label="Message", placeholder="Type here...")
92
+ with gr.Row():
93
+ submit_btn = gr.Button("Send", variant="primary")
94
+ clear_btn = gr.ClearButton([msg, chatbot])
95
+
96
+ msg.submit(chat, [msg, chatbot, model_choice], chatbot)
97
+ submit_btn.click(chat, [msg, chatbot, model_choice], chatbot)
98
 
99
+ demo.launch()