DDDDEvvvvv commited on
Commit
18dce4f
·
verified ·
1 Parent(s): 72c463c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -88
app.py CHANGED
@@ -1,101 +1,45 @@
1
  import gradio as gr
 
2
  import torch
3
- from transformers import AutoModelForCausalLM, AutoTokenizer
4
 
5
- # ------------------ MODEL SETUP ------------------
6
- MODEL_NAME = "microsoft/Phi-3-mini-4k-instruct"
 
7
 
8
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
9
- model = AutoModelForCausalLM.from_pretrained(
10
- MODEL_NAME,
11
- torch_dtype=torch.float32,
12
- device_map="cpu"
13
- )
14
- model.eval()
15
 
16
- SYSTEM_PROMPT = """You are a calm, intelligent assistant.
17
- Respond ONLY to the user's last message.
18
- Do NOT assume intent or emotions.
19
- If the message is unclear, ask for clarification.
20
- Keep responses natural and concise.
21
- """
22
-
23
- # ------------------ CHAT FUNCTIONS ------------------
24
-
25
- def build_prompt(history):
26
- prompt = SYSTEM_PROMPT + "\n\n"
27
- for msg in history:
28
- role = "User" if msg["role"] == "user" else "Assistant"
29
- prompt += f"{role}: {msg['content']}\n"
30
- prompt += "Assistant:"
31
- return prompt
32
-
33
- def respond(message, history):
34
- # Add user message
35
  history.append({"role": "user", "content": message})
36
-
37
- # Add loading placeholder
38
- history.append({"role": "assistant", "content": "Thinking..."})
39
- yield history, history
40
-
41
- # Build prompt for model
42
- prompt = build_prompt(history[:-1])
43
-
44
- inputs = tokenizer(
45
- prompt,
46
- return_tensors="pt",
47
- truncation=True,
48
- max_length=2048
49
- )
50
-
51
- # Generate response
52
- with torch.no_grad():
53
- output = model.generate(
54
- **inputs,
55
- max_new_tokens=120,
56
- do_sample=False,
57
- temperature=0.0,
58
- top_p=1.0,
59
- repetition_penalty=1.1
60
- )
61
-
62
- text = tokenizer.decode(output[0], skip_special_tokens=True)
63
- # Extract model response
64
- response = text.split("Assistant:")[-1].strip()
65
-
66
- # Replace loading with actual response
67
- history[-1]["content"] = response
68
-
69
- # Trim history if too long
70
- if len(history) > 10:
71
- history = history[-8:]
72
-
73
- yield history, history
74
 
75
  def reset_chat():
76
- return [], []
77
-
78
- # ------------------ GRADIO UI ------------------
79
-
80
- with gr.Blocks(css="""
81
- body { background-color: #000; color: #fff; }
82
- .gr-chatbot { background-color: #111; border-radius: 12px; }
83
- .gr-chatbot .message.user { border-color: #0ff; background-color: transparent; }
84
- .gr-chatbot .message.bot { border-color: #aaa; background-color: transparent; }
85
- .gr-textbox textarea { background-color: transparent; color: #fff; border: 1px solid #555; }
86
- .gr-textbox textarea::selection { background-color: #0ff; color: #000; }
87
- .gr-button { background-color: #0ff; color: #000; border-radius: 8px; }
88
- footer { display: none; }
89
- """) as demo:
90
 
91
- state = gr.State([])
92
  chatbot = gr.Chatbot(label="DevMegaBlack")
93
  msg = gr.Textbox(placeholder="Say something...")
94
- reset = gr.Button("Reset Chat")
95
-
96
- # Submit message
97
- msg.submit(respond, [msg, state], [chatbot, state]).then(lambda: "", None, msg)
98
- reset.click(reset_chat, [], [chatbot, state])
99
 
100
- demo.queue()
101
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
  import torch
 
4
 
5
+ model_name = "facebook/blenderbot-400M-distill"
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
8
 
9
+ history = []
 
 
 
 
 
 
10
 
11
+ def respond(message):
12
+ global history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  history.append({"role": "user", "content": message})
14
+ last_msgs = history[-3:]
15
+ input_text = " ".join([m["content"] for m in last_msgs])
16
+ inputs = tokenizer(input_text, return_tensors="pt")
17
+ outputs = model.generate(**inputs, max_new_tokens=100)
18
+ response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
19
+ history.append({"role": "assistant", "content": response_text})
20
+ return history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def reset_chat():
23
+ global history
24
+ history = []
25
+ return history
26
+
27
+ css = """
28
+ body {background-color: #000 !important; color: #fff !important;}
29
+ .gr-chatbot {background-color: #111 !important; border-radius: 12px; height: 100% !important;}
30
+ .gr-chatbot .message.user {border-color: #0ff; background-color: transparent !important;}
31
+ .gr-chatbot .message.bot {border-color: #aaa; background-color: transparent !important;}
32
+ .gr-textbox textarea {background-color: transparent !important; color: #fff !important; border: 1px solid #555 !important;}
33
+ .gr-textbox textarea::selection {background-color: #0ff !important; color: #000 !important;}
34
+ .gr-button {background-color: #0ff !important; color: #000 !important; border-radius: 8px;}
35
+ footer {display: none !important;}
36
+ """
37
 
38
+ with gr.Blocks(css=css) as demo:
39
  chatbot = gr.Chatbot(label="DevMegaBlack")
40
  msg = gr.Textbox(placeholder="Say something...")
41
+ reset_btn = gr.Button("Reset Chat")
42
+ msg.submit(respond, msg, chatbot)
43
+ reset_btn.click(reset_chat, [], chatbot)
 
 
44
 
 
45
  demo.launch()