Spaces:
Sleeping
Sleeping
update model
Browse files- Dockerfile +0 -2
- app.py +118 -126
- requirements.txt +3 -5
Dockerfile
CHANGED
@@ -6,8 +6,6 @@ COPY ./requirements.txt /code/requirements.txt
|
|
6 |
|
7 |
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
|
9 |
-
RUN pip install -U gptqmodel --no-build-isolation -v
|
10 |
-
|
11 |
# Set up a new user named "user" with user ID 1000
|
12 |
RUN useradd -m -u 1000 user
|
13 |
# Switch to the "user" user
|
|
|
6 |
|
7 |
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
|
|
|
|
|
9 |
# Set up a new user named "user" with user ID 1000
|
10 |
RUN useradd -m -u 1000 user
|
11 |
# Switch to the "user" user
|
app.py
CHANGED
@@ -1,127 +1,119 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
)
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
# # Example usage
|
121 |
-
# prompt = "Explain quantum computing in simple terms."
|
122 |
-
# response = generate_response(prompt)
|
123 |
-
# print(response)
|
124 |
-
|
125 |
-
|
126 |
-
if __name__ == "__main__":
|
127 |
generate_response.launch()
|
|
|
1 |
+
import inspect
|
2 |
+
from typing import get_type_hints, Callable, Any
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
+
|
6 |
+
model_id = "unsloth/SmolLM2-135M-Instruct-GGUF"
|
7 |
+
filename = "SmolLM2-135M-Instruct-Q8_0.gguf"
|
8 |
+
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, gguf_file=filename)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, gguf_file=filename)
|
11 |
+
|
12 |
+
|
13 |
+
def parse_docstring(func):
|
14 |
+
doc = inspect.getdoc(func)
|
15 |
+
if not doc:
|
16 |
+
return {"title": "Untitled", "description": ""}
|
17 |
+
|
18 |
+
lines = doc.splitlines()
|
19 |
+
title = next((line.replace("Title:", "").strip() for line in lines if line.startswith("Title:")), "Untitled")
|
20 |
+
description = "\n".join(line.strip() for line in lines if line.startswith("Description:"))
|
21 |
+
description = description.replace("Description:", "").strip()
|
22 |
+
|
23 |
+
return {"title": title, "description": description}
|
24 |
+
|
25 |
+
def gradio_app_with_docs(func: Callable) -> Callable:
|
26 |
+
sig = inspect.signature(func)
|
27 |
+
type_hints = get_type_hints(func)
|
28 |
+
metadata = parse_docstring(func)
|
29 |
+
|
30 |
+
"""
|
31 |
+
A decorator that automatically builds and launches a Gradio interface
|
32 |
+
based on function type hints.
|
33 |
+
|
34 |
+
Args:
|
35 |
+
func: A callable with type-hinted parameters and return type.
|
36 |
+
|
37 |
+
Returns:
|
38 |
+
The wrapped function with a `.launch()` method to start the app.
|
39 |
+
"""
|
40 |
+
# Infer Gradio components from type hints
|
41 |
+
def _map_type(t: type) -> gr.Component:
|
42 |
+
if t == str:
|
43 |
+
return gr.Textbox(label="Input")
|
44 |
+
elif t == int:
|
45 |
+
return gr.Number(precision=0)
|
46 |
+
elif t == float:
|
47 |
+
return gr.Number()
|
48 |
+
elif t == bool:
|
49 |
+
return gr.Checkbox()
|
50 |
+
elif hasattr(t, "__origin__") and t.__origin__ == list: # Handle List[type]
|
51 |
+
elem_type = t.__args__[0]
|
52 |
+
if elem_type == str:
|
53 |
+
return gr.Dropdown(choices=["Option1", "Option2"])
|
54 |
+
else:
|
55 |
+
raise ValueError(f"Unsupported list element type: {elem_type}")
|
56 |
+
else:
|
57 |
+
raise ValueError(f"Unsupported type: {t}")
|
58 |
+
|
59 |
+
# Extract function signature and type hints
|
60 |
+
sig = inspect.signature(func)
|
61 |
+
type_hints = get_type_hints(func)
|
62 |
+
|
63 |
+
# Map parameters to Gradio inputs
|
64 |
+
inputs = []
|
65 |
+
for name, param in sig.parameters.items():
|
66 |
+
if name == "self":
|
67 |
+
continue # Skip self in class methods
|
68 |
+
param_type = type_hints.get(name, Any)
|
69 |
+
component = _map_type(param_type)
|
70 |
+
component.label = name.replace("_", " ").title()
|
71 |
+
inputs.append(component)
|
72 |
+
|
73 |
+
# Map return type to Gradio output
|
74 |
+
return_type = type_hints.get("return", Any)
|
75 |
+
outputs = _map_type(return_type)
|
76 |
+
|
77 |
+
# Wrap function with Gradio interface
|
78 |
+
interface = gr.Interface(fn=func, inputs=inputs, outputs=outputs)
|
79 |
+
|
80 |
+
with gr.Blocks() as demo:
|
81 |
+
gr.Markdown(f"## {metadata['title']}\n{metadata['description']}")
|
82 |
+
interface = gr.Interface(fn=func, inputs=inputs, outputs=outputs)
|
83 |
+
|
84 |
+
def wrapper(*args, **kwargs):
|
85 |
+
return func(*args, **kwargs)
|
86 |
+
|
87 |
+
wrapper.launch = lambda: demo.launch()
|
88 |
+
return wrapper
|
89 |
+
|
90 |
+
|
91 |
+
@gradio_app_with_docs
|
92 |
+
def generate_response(prompt: str) -> str:
|
93 |
+
"""
|
94 |
+
Title: Super Tiny GPTQ V2 Model on CPU
|
95 |
+
Description: A Simple app to test out the potentials of small GPTQ LLM model.
|
96 |
+
|
97 |
+
Args:
|
98 |
+
prompt (str): A simple prompt.
|
99 |
+
|
100 |
+
Returns:
|
101 |
+
str: Simplified response.
|
102 |
+
"""
|
103 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cpu") # Move inputs to CPU
|
104 |
+
outputs = model.generate(
|
105 |
+
**inputs,
|
106 |
+
max_new_tokens=50,
|
107 |
+
temperature=0.7,
|
108 |
+
top_p=0.9
|
109 |
+
)
|
110 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
111 |
+
|
112 |
+
# # Example usage
|
113 |
+
# prompt = "Explain quantum computing in simple terms."
|
114 |
+
# response = generate_response(prompt)
|
115 |
+
# print(response)
|
116 |
+
|
117 |
+
|
118 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
generate_response.launch()
|
requirements.txt
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
transformers
|
5 |
-
accelerate
|
|
|
1 |
+
numpy==2.2.5
|
2 |
+
gguf
|
3 |
+
transformers
|
|
|
|