Muhammadidrees commited on
Commit
caaa62b
Β·
verified Β·
1 Parent(s): d45b097

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -50
app.py CHANGED
@@ -99,56 +99,56 @@ def respond(age, gender, height, weight,
99
  f"- Plasma (PV): {pv} mL"
100
  )
101
 
102
- # --- Model Inference + Streaming ---
103
- try:
104
- messages = [
105
- {"role": "system", "content": system_message},
106
- {"role": "user", "content": user_message}
107
- ]
108
-
109
- # Tokenize messages
110
- encodings = tokenizer.apply_chat_template(
111
- messages,
112
- return_tensors="pt",
113
- add_special_tokens=True,
114
- padding=True,
115
- truncation=True
116
- )
117
-
118
- input_ids = encodings["input_ids"].to(model.device)
119
- attention_mask = encodings["attention_mask"].to(model.device) # βœ… Correct attention mask
120
-
121
- # Streamer for capturing tokens
122
- streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
123
-
124
- generation_kwargs = dict(
125
- input_ids=input_ids,
126
- attention_mask=attention_mask,
127
- max_new_tokens=2000,
128
- temperature=0.3,
129
- top_p=0.9,
130
- do_sample=True,
131
- streamer=streamer,
132
- pad_token_id=tokenizer.pad_token_id
133
- )
134
-
135
- # Run model in separate thread
136
- thread = threading.Thread(target=model.generate, kwargs=generation_kwargs)
137
- thread.start()
138
-
139
- # Collect tokens into full response
140
- reply = ""
141
- for token in streamer:
142
- reply += token
143
-
144
- # Convert Markdown β†’ HTML
145
- html_output = markdown.markdown(
146
- reply,
147
- extensions=["tables", "fenced_code", "nl2br"]
148
- )
149
-
150
- except Exception as e:
151
- html_output = f"<p style='color:red;'>⚠️ Error: {str(e)}</p>"
152
 
153
 
154
 
 
99
  f"- Plasma (PV): {pv} mL"
100
  )
101
 
102
+ # --- Model Inference + Streaming ---
103
+ try:
104
+ messages = [
105
+ {"role": "system", "content": system_message},
106
+ {"role": "user", "content": user_message}
107
+ ]
108
+
109
+ # Tokenize messages
110
+ encodings = tokenizer.apply_chat_template(
111
+ messages,
112
+ return_tensors="pt",
113
+ add_special_tokens=True,
114
+ padding=True,
115
+ truncation=True
116
+ )
117
+
118
+ input_ids = encodings["input_ids"].to(model.device)
119
+ attention_mask = encodings["attention_mask"].to(model.device) # βœ… Correct attention mask
120
+
121
+ # Streamer for capturing tokens
122
+ streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
123
+
124
+ generation_kwargs = dict(
125
+ input_ids=input_ids,
126
+ attention_mask=attention_mask,
127
+ max_new_tokens=2000,
128
+ temperature=0.3,
129
+ top_p=0.9,
130
+ do_sample=True,
131
+ streamer=streamer,
132
+ pad_token_id=tokenizer.pad_token_id
133
+ )
134
+
135
+ # Run model in separate thread
136
+ thread = threading.Thread(target=model.generate, kwargs=generation_kwargs)
137
+ thread.start()
138
+
139
+ # Collect tokens into full response
140
+ reply = ""
141
+ for token in streamer:
142
+ reply += token
143
+
144
+ # Convert Markdown β†’ HTML
145
+ html_output = markdown.markdown(
146
+ reply,
147
+ extensions=["tables", "fenced_code", "nl2br"]
148
+ )
149
+
150
+ except Exception as e:
151
+ html_output = f"<p style='color:red;'>⚠️ Error: {str(e)}</p>"
152
 
153
 
154