Spaces:
Runtime error
Runtime error
kokofixcomputers
commited on
Commit
Β·
641c30d
1
Parent(s):
5167777
Update space
Browse files
app.py
CHANGED
|
@@ -1,70 +1,52 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
system_message,
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
for message in client.chat_completion(
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
temperature=temperature,
|
| 32 |
top_p=top_p,
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
type="messages",
|
| 49 |
-
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Load a small DeepSeek Coder model suitable for CPU and limited RAM usage
|
| 6 |
+
model_name = "deepseek-ai/deepseek-coder-1.3b-base" # Change to smaller model for your RAM if needed
|
| 7 |
|
| 8 |
+
# Load tokenizer and model
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Put model in eval mode (no training)
|
| 13 |
+
model.eval()
|
| 14 |
|
| 15 |
+
def generate_code(prompt, max_tokens, temperature, top_p):
|
| 16 |
+
# Tokenize input prompt
|
| 17 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 18 |
|
| 19 |
+
# Generate output tokens
|
| 20 |
+
outputs = model.generate(
|
| 21 |
+
**inputs,
|
| 22 |
+
max_new_tokens=max_tokens,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
temperature=temperature,
|
| 24 |
top_p=top_p,
|
| 25 |
+
do_sample=True,
|
| 26 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 27 |
+
)
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Decode generated tokens to string
|
| 30 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 31 |
|
| 32 |
+
# Return generated completion excluding the input prompt for clarity
|
| 33 |
+
return generated_text[len(prompt):].strip()
|
| 34 |
|
| 35 |
+
# Gradio app interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
with gr.Blocks() as demo:
|
| 37 |
+
gr.Markdown("# DeepSeek Coder Chatbot")
|
| 38 |
+
prompt_input = gr.Textbox(label="Code Prompt", lines=5, placeholder="Write your code prompt here...")
|
| 39 |
+
max_tokens_slider = gr.Slider(1, 1024, value=512, step=1, label="Max Generated Tokens")
|
| 40 |
+
temperature_slider = gr.Slider(0.1, 1.0, value=0.7, step=0.05, label="Temperature")
|
| 41 |
+
top_p_slider = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p (nucleus sampling)")
|
| 42 |
+
generate_btn = gr.Button("Generate Code")
|
| 43 |
+
output = gr.Textbox(label="Generated Code", lines=15)
|
| 44 |
+
|
| 45 |
+
generate_btn.click(
|
| 46 |
+
fn=generate_code,
|
| 47 |
+
inputs=[prompt_input, max_tokens_slider, temperature_slider, top_p_slider],
|
| 48 |
+
outputs=output,
|
| 49 |
+
)
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
| 52 |
demo.launch()
|