Guilherme34 commited on
Commit
d7c66c8
·
verified ·
1 Parent(s): 6b7b9c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -44
app.py CHANGED
@@ -1,4 +1,4 @@
1
- """Gradio interface for nanochat model (using Guilherme34 repo)."""
2
 
3
  from __future__ import annotations
4
 
@@ -12,37 +12,18 @@ from huggingface_hub import snapshot_download
12
 
13
  from model import NanochatModel
14
 
15
- # --- Configuration: point to YOUR repo and subfolders ---
16
- # Defaults can still be overridden by env vars if you want.
17
- MODEL_REPO = os.environ.get("MODEL_REPO", "Guilherme34/nanochat-retrained-pytorch")
18
  MODEL_DIR = os.environ.get("MODEL_DIR", "./model_cache")
19
-
20
- # The model files live in "sft/" and the tokenizer in "tokenizer/" inside the repo.
21
- # We'll mirror that structure locally under MODEL_DIR.
22
- MODEL_SUBDIR = "sft"
23
- TOKENIZER_SUBDIR = "tokenizer"
24
-
25
  _model: NanochatModel | None = None
26
 
27
 
28
  def download_model() -> None:
29
- """Download the model + tokenizer folders from Hugging Face if needed."""
30
  model_path = Path(MODEL_DIR)
31
- sft_path = model_path / MODEL_SUBDIR
32
- tok_path = model_path / TOKENIZER_SUBDIR
33
-
34
- # If either folder is missing/empty, fetch both to keep them in sync.
35
- need_download = (
36
- not sft_path.exists() or not any(sft_path.iterdir()) or
37
- not tok_path.exists() or not any(tok_path.iterdir())
38
- )
39
-
40
- if need_download:
41
- # Only pull what we need to keep downloads light.
42
  snapshot_download(
43
  repo_id=MODEL_REPO,
44
  local_dir=MODEL_DIR,
45
- allow_patterns=[f"{MODEL_SUBDIR}/**", f"{TOKENIZER_SUBDIR}/**"],
46
  )
47
 
48
 
@@ -51,13 +32,6 @@ def load_model() -> None:
51
  global _model
52
  if _model is None:
53
  download_model()
54
- # If your NanochatModel supports passing a tokenizer_dir, uncomment and use it:
55
- # _model = NanochatModel(
56
- # model_dir=str(Path(MODEL_DIR) / MODEL_SUBDIR),
57
- # tokenizer_dir=str(Path(MODEL_DIR) / TOKENIZER_SUBDIR),
58
- # device="cpu",
59
- # )
60
- # Otherwise, keep the original and let your class discover subfolders:
61
  _model = NanochatModel(model_dir=MODEL_DIR, device="cpu")
62
 
63
 
@@ -69,7 +43,7 @@ def respond(
69
  history: list[dict[str, str]],
70
  temperature: float,
71
  top_k: int,
72
- system_prompt: str,
73
  ) -> Generator[str, Any, None]:
74
  """Generate a response using the nanochat model.
75
 
@@ -85,11 +59,10 @@ def respond(
85
  """
86
  conversation: list[dict[str, str]] = []
87
 
88
- # Only include a system message if one was actually provided
89
- if system_prompt and system_prompt.strip():
90
- conversation.append({"role": "system", "content": system_prompt.strip()})
91
 
92
- # Replay prior turns (already in gradio "messages" format)
93
  for msg in history:
94
  conversation.append(msg)
95
 
@@ -112,19 +85,24 @@ chatbot = gr.ChatInterface(
112
  type="messages",
113
  additional_inputs=[
114
  gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
115
- gr.Slider(minimum=1, maximum=200, value=50, step=1, label="Top-k sampling"),
116
- gr.Textbox(
 
 
 
 
 
 
117
  label="System message (optional)",
118
  placeholder="e.g., You are a concise assistant that answers in markdown.",
119
  lines=3,
120
- value="", # ensure we always pass a string
121
  ),
122
  ],
123
  )
124
 
125
- with gr.Blocks(title="nanochat (Guilherme34)") as demo:
126
- gr.Markdown("# nanochat (retrained, PyTorch)")
127
- gr.Markdown("Using model weights from `sft/` and tokenizer from `tokenizer/` in your repo.")
128
  gr.Markdown(
129
  "**Note:** This model is a research experiment. "
130
  "Obviously do not rely on the outputs!",
@@ -133,6 +111,4 @@ with gr.Blocks(title="nanochat (Guilherme34)") as demo:
133
 
134
 
135
  if __name__ == "__main__":
136
- # To expose publicly or change port, use:
137
- # demo.launch(server_name='0.0.0.0', server_port=7860, share=True)
138
- demo.launch()
 
1
+ """Gradio interface for nanochat model."""
2
 
3
  from __future__ import annotations
4
 
 
12
 
13
  from model import NanochatModel
14
 
15
+ MODEL_REPO = os.environ.get("MODEL_REPO", "Guilherme34/nanochat-retrained-pytorch-duplicated")
 
 
16
  MODEL_DIR = os.environ.get("MODEL_DIR", "./model_cache")
 
 
 
 
 
 
17
  _model: NanochatModel | None = None
18
 
19
 
20
  def download_model() -> None:
21
+ """Download the model from Hugging Face if needed."""
22
  model_path = Path(MODEL_DIR)
23
+ if not model_path.exists() or not any(model_path.iterdir()):
 
 
 
 
 
 
 
 
 
 
24
  snapshot_download(
25
  repo_id=MODEL_REPO,
26
  local_dir=MODEL_DIR,
 
27
  )
28
 
29
 
 
32
  global _model
33
  if _model is None:
34
  download_model()
 
 
 
 
 
 
 
35
  _model = NanochatModel(model_dir=MODEL_DIR, device="cpu")
36
 
37
 
 
43
  history: list[dict[str, str]],
44
  temperature: float,
45
  top_k: int,
46
+ system_prompt: str, # NEW
47
  ) -> Generator[str, Any, None]:
48
  """Generate a response using the nanochat model.
49
 
 
59
  """
60
  conversation: list[dict[str, str]] = []
61
 
62
+ # If a system message is provided, put it at the start of the conversation.
63
+ conversation.append({"role": "system", "content": system_prompt.strip()})
 
64
 
65
+ # Replay prior turns
66
  for msg in history:
67
  conversation.append(msg)
68
 
 
85
  type="messages",
86
  additional_inputs=[
87
  gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature"),
88
+ gr.Slider(
89
+ minimum=1,
90
+ maximum=200,
91
+ value=50,
92
+ step=1,
93
+ label="Top-k sampling",
94
+ ),
95
+ gr.Textbox( # NEW
96
  label="System message (optional)",
97
  placeholder="e.g., You are a concise assistant that answers in markdown.",
98
  lines=3,
 
99
  ),
100
  ],
101
  )
102
 
103
+ with gr.Blocks(title="nanochat") as demo:
104
+ gr.Markdown("# nanochat")
105
+ gr.Markdown("Chat with an AI trained in 4 hours for $100")
106
  gr.Markdown(
107
  "**Note:** This model is a research experiment. "
108
  "Obviously do not rely on the outputs!",
 
111
 
112
 
113
  if __name__ == "__main__":
114
+ demo.launch()