asimcodeml commited on
Commit
bcd43dd
·
verified ·
1 Parent(s): b275741

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
+
4
+ # --- Load Model ---
5
+ MODEL_PATH = "./tinyllama-jobskills-final_update_4" # Model files are in the repo root
6
+
7
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ MODEL_PATH,
10
+ trust_remote_code=True,
11
+ device_map="auto"
12
+ )
13
+
14
+ pipe = pipeline(
15
+ "text-generation",
16
+ model=model,
17
+ tokenizer=tokenizer,
18
+ device_map="auto"
19
+ )
20
+
21
+ # --- Define Chat Function ---
22
+ def chat_fn(message, history):
23
+ history_text = ""
24
+ for user, bot in history:
25
+ history_text += f"User: {user}\nAssistant: {bot}\n"
26
+ history_text += f"User: {message}\nAssistant:"
27
+
28
+ # generate response
29
+ response = pipe(
30
+ history_text,
31
+ max_new_tokens=256,
32
+ do_sample=True,
33
+ temperature=0.7,
34
+ top_p=0.9
35
+ )[0]["generated_text"]
36
+
37
+ # extract assistant reply
38
+ reply = response.split("Assistant:")[-1].strip()
39
+ return reply
40
+
41
+ # --- Gradio UI ---
42
+ with gr.Blocks() as demo:
43
+ gr.Markdown("## 🚀 Chat with My Custom Model")
44
+
45
+ chatbot = gr.Chatbot()
46
+ msg = gr.Textbox(label="Type your message")
47
+ clear = gr.Button("Clear")
48
+
49
+ def user_fn(user_message, chat_history):
50
+ bot_message = chat_fn(user_message, chat_history)
51
+ chat_history.append((user_message, bot_message))
52
+ return "", chat_history
53
+
54
+ msg.submit(user_fn, [msg, chatbot], [msg, chatbot])
55
+ clear.click(lambda: None, None, chatbot, queue=False)
56
+
57
+ # --- Launch ---
58
+ if __name__ == "__main__":
59
+ demo.launch()