Abeersherif commited on
Commit
6162c50
·
verified ·
1 Parent(s): a328e4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -25
app.py CHANGED
@@ -1,10 +1,9 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
3
 
4
- # ✅ Your fine-tuned model on Hugging Face
5
  MODEL_NAME = "Abeersherif/Medical_Homework2"
6
 
7
- # Load model & tokenizer from HF
8
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
9
  model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
10
 
@@ -15,37 +14,40 @@ pipe = pipeline(
15
  )
16
 
17
  def respond(message, history, system_message, max_tokens, temperature, top_p):
18
- # Combine chat history manually (since no chat template exists)
19
- full_prompt = f"System: {system_message}\n"
20
- for turn in history:
21
- role = turn["role"]
22
- text = turn["content"]
23
- full_prompt += f"{role.capitalize()}: {text}\n"
24
-
25
- full_prompt += f"User: {message}\nAssistant:"
26
-
27
- out = pipe(
28
- full_prompt,
29
- max_new_tokens=max_tokens,
30
- temperature=temperature,
31
- top_p=top_p,
 
 
32
  do_sample=True,
33
  )[0]["generated_text"]
34
 
35
- # return only assistant after last "Assistant:"
36
- if "Assistant:" in out:
37
- out = out.split("Assistant:", 1)[-1]
38
-
39
- return out.strip()
40
 
 
41
 
42
 
43
  chatbot = gr.ChatInterface(
44
  fn=respond,
45
- type="messages",
46
  additional_inputs=[
47
- gr.Textbox("You are a helpful medical assistant. Answer concisely with brief reasoning.",
48
- label="System message"),
 
 
49
  gr.Slider(1, 512, value=256, step=1, label="Max new tokens"),
50
  gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature"),
51
  gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p"),
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
 
 
4
  MODEL_NAME = "Abeersherif/Medical_Homework2"
5
 
6
+ # Load model and tokenizer
7
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
8
  model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
9
 
 
14
  )
15
 
16
  def respond(message, history, system_message, max_tokens, temperature, top_p):
17
+ """
18
+ message: str -> latest user message
19
+ history: list[[user, bot], ...] -> previous chat turns (default tuples mode)
20
+ """
21
+
22
+ # Build a simple text prompt (no chat template)
23
+ conversation = f"System: {system_message}\n\n"
24
+ for user_msg, bot_msg in history:
25
+ conversation += f"User: {user_msg}\nAssistant: {bot_msg}\n"
26
+ conversation += f"User: {message}\nAssistant:"
27
+
28
+ result = pipe(
29
+ conversation,
30
+ max_new_tokens=int(max_tokens),
31
+ temperature=float(temperature),
32
+ top_p=float(top_p),
33
  do_sample=True,
34
  )[0]["generated_text"]
35
 
36
+ # Keep only what the assistant said last
37
+ if "Assistant:" in result:
38
+ result = result.split("Assistant:")[-1]
 
 
39
 
40
+ return result.strip()
41
 
42
 
43
  chatbot = gr.ChatInterface(
44
  fn=respond,
45
+ # ⚠️ use default history format (tuples), do NOT set type="messages" here
46
  additional_inputs=[
47
+ gr.Textbox(
48
+ "You are a helpful medical assistant. Answer concisely with brief reasoning.",
49
+ label="System message",
50
+ ),
51
  gr.Slider(1, 512, value=256, step=1, label="Max new tokens"),
52
  gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature"),
53
  gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p"),