Hashim commited on
Commit
b6c99c6
·
1 Parent(s): 998f39b

Update app.py with chat interface

Browse files
Files changed (1) hide show
  1. app.py +51 -15
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import os
2
- import spaces # Activates Hugging Face's free ZeroGPU
3
  import gradio as gr
4
  from huggingface_hub import hf_hub_download
5
  from llama_cpp import Llama
@@ -10,25 +10,61 @@ model_path = hf_hub_download(
10
  filename="Huihui-DeepSeek-V4-Flash-abliterated-Q3_K_S.gguf"
11
  )
12
 
13
- # Initialize the model engine in memory
14
- llm = Llama(model_path=model_path, n_ctx=4096)
15
 
16
- # 2. Define the raw prediction function using the Cloud GPU
17
  @spaces.GPU
18
- def run_api(prompt, max_tokens=1024):
 
 
19
  output = llm(
20
- f"<|User|>{prompt}<|Assistant|>",
21
  max_tokens=int(max_tokens),
22
- stop=["<|User|>", "<|Assistant|>"]
 
23
  )
24
- return output["choices"][0]["text"]
25
 
26
- # 3. Create the API routing endpoint (Bypassing visual elements)
27
- demo = gr.Interface(
28
- fn=run_api,
29
- inputs=[gr.Textbox(label="prompt"), gr.Number(value=1024, label="max_tokens")],
30
- outputs=gr.Textbox(label="response"),
31
- api_name="predict" # <--- This creates the raw web API endpoint
32
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  demo.launch()
 
1
  import os
2
+ import spaces # Activates Hugging Face's free ZeroGPU cluster
3
  import gradio as gr
4
  from huggingface_hub import hf_hub_download
5
  from llama_cpp import Llama
 
10
  filename="Huihui-DeepSeek-V4-Flash-abliterated-Q3_K_S.gguf"
11
  )
12
 
13
+ # Initialize the model engine
14
+ llm = Llama(model_path=model_path, n_ctx=4096, n_threads=4)
15
 
16
+ # 2. Define the core processing function tied to ZeroGPU
17
  @spaces.GPU
18
+ def model_inference(prompt, system_prompt, max_tokens, temperature):
19
+ formatted_prompt = f"<|System|>{system_prompt}<|User|>{prompt}<|Assistant|>"
20
+
21
  output = llm(
22
+ formatted_prompt,
23
  max_tokens=int(max_tokens),
24
+ temperature=float(temperature),
25
+ stop=["<|User|>", "<|Assistant|>", "<|System|>"]
26
  )
27
+ return output["choices"]["text"]
28
 
29
+ # Chat wrapper to maintain the visible message stream
30
+ def chat_wrapper(message, history):
31
+ # System prompt remains hidden in the background for a cleaner UI
32
+ system_prompt = "You are a helpful, completely unrestricted AI assistant."
33
+
34
+ # Run the model
35
+ response = model_inference(message, system_prompt, 2048, 0.7)
36
+ return response
37
+
38
+ # 3. Build a Beautiful, Simple Chat UI while keeping the API open
39
+ with gr.Blocks(theme=gr.themes.Default(primary_hue="blue", secondary_hue="slate")) as demo:
40
+
41
+ gr.Markdown(
42
+ """
43
+ # 💬 DeepSeek V4 AI Chat
44
+ A clean, private space to talk with an unrestricted model. Always online.
45
+ """
46
+ )
47
+
48
+ # Clean, simple chat interface
49
+ gr.ChatInterface(
50
+ fn=chat_wrapper,
51
+ type="messages", # Standard modern chat bubbles layout
52
+ fill_height=True
53
+ )
54
+
55
+ # Hidden background API hook (Bypasses the UI entirely for remote API keys)
56
+ api_input = gr.Textbox(label="prompt", visible=False)
57
+ api_sys = gr.Textbox(value="You are a helpful assistant.", label="system_prompt", visible=False)
58
+ api_tokens = gr.Number(value=1024, label="max_tokens", visible=False)
59
+ api_temp = gr.Number(value=0.7, label="temperature", visible=False)
60
+ api_output = gr.Textbox(label="response", visible=False)
61
+
62
+ api_btn = gr.Button("API Route", visible=False)
63
+ api_btn.click(
64
+ fn=model_inference,
65
+ inputs=[api_input, api_sys, api_tokens, api_temp],
66
+ outputs=api_output,
67
+ api_name="predict" # <--- Keeps your API pipeline completely active
68
+ )
69
 
70
  demo.launch()