druvx13 commited on
Commit
0057689
·
verified ·
1 Parent(s): 9028e44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -19
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  from llama_cpp import Llama
 
3
  import os
4
 
5
  # Model configuration
@@ -10,35 +11,52 @@ MAX_TOKENS = 200
10
 
11
  # Initialize model (loads once at startup)
12
  def load_model():
 
13
  os.makedirs(CACHE_DIR, exist_ok=True)
 
 
 
 
 
 
 
 
 
14
  return Llama(
15
- model_path=None, # Auto-download from HF
16
- hf_repo=MODEL_REPO,
17
- hf_file=MODEL_FILE,
18
- n_ctx=2048, # Context length
19
- n_threads=4, # CPU threads
20
- verbose=False # Disable debug logs
21
  )
22
 
 
23
  llm = load_model()
24
 
25
- # Generation function
26
  def generate_text(prompt, max_tokens=MAX_TOKENS, temp=0.7, top_p=0.95):
27
- output = llm(
28
- prompt=prompt,
29
- max_tokens=max_tokens,
30
- temperature=temp,
31
- top_p=top_p,
32
- echo=False
33
- )
34
- return output["choices"][0]["text"]
 
 
 
 
35
 
36
- # UI components
37
  with gr.Blocks(theme="soft") as demo:
38
- gr.Markdown("# GPT2 Text Generator (GGUF Version)\nType a prompt and generate text using the quantized GPT2 model.")
 
 
 
39
 
40
  with gr.Row():
41
  with gr.Column():
 
42
  prompt = gr.Textbox(
43
  label="Input Prompt",
44
  placeholder="Enter your prompt here...",
@@ -56,7 +74,7 @@ with gr.Blocks(theme="soft") as demo:
56
  maximum=1.0,
57
  value=0.7,
58
  step=0.1,
59
- label="Temperature"
60
  )
61
  top_p = gr.Slider(
62
  minimum=0.1,
@@ -67,13 +85,16 @@ with gr.Blocks(theme="soft") as demo:
67
  )
68
 
69
  with gr.Column():
 
70
  output = gr.Textbox(label="Generated Text", lines=10)
71
- generate_btn = gr.Button("Generate", variant="primary")
72
 
 
73
  generate_btn.click(
74
  fn=generate_text,
75
  inputs=[prompt, max_tokens, temp, top_p],
76
  outputs=output
77
  )
78
 
 
79
  demo.launch()
 
1
  import gradio as gr
2
  from llama_cpp import Llama
3
+ from huggingface_hub import hf_hub_download
4
  import os
5
 
6
  # Model configuration
 
11
 
12
  # Initialize model (loads once at startup)
13
  def load_model():
14
+ """Download and load GGUF model with proper path handling"""
15
  os.makedirs(CACHE_DIR, exist_ok=True)
16
+
17
+ # Download model if not cached
18
+ model_path = hf_hub_download(
19
+ repo_id=MODEL_REPO,
20
+ filename=MODEL_FILE,
21
+ cache_dir=CACHE_DIR,
22
+ force_download=False # Set to True to bypass cache
23
+ )
24
+
25
  return Llama(
26
+ model_path=model_path, # Now a valid path string
27
+ n_ctx=2048, # Context window size
28
+ n_threads=4, # CPU threads for faster inference
29
+ verbose=False # Disable debug logs
 
 
30
  )
31
 
32
+ # Load model at startup
33
  llm = load_model()
34
 
35
+ # Generation function with parameters
36
  def generate_text(prompt, max_tokens=MAX_TOKENS, temp=0.7, top_p=0.95):
37
+ """Generate text using GGUF model with parameter control"""
38
+ try:
39
+ output = llm(
40
+ prompt=prompt,
41
+ max_tokens=max_tokens,
42
+ temperature=temp,
43
+ top_p=top_p,
44
+ echo=False # Don't repeat input in output
45
+ )
46
+ return output["choices"][0]["text"]
47
+ except Exception as e:
48
+ return f"Error generating text: {str(e)}"
49
 
50
+ # UI Components
51
  with gr.Blocks(theme="soft") as demo:
52
+ gr.Markdown("""
53
+ # 🧠 GPT2 Text Generator (GGUF Version)
54
+ Enter a prompt and adjust parameters to generate AI text using the quantized GPT2 model.
55
+ """)
56
 
57
  with gr.Row():
58
  with gr.Column():
59
+ # Input components
60
  prompt = gr.Textbox(
61
  label="Input Prompt",
62
  placeholder="Enter your prompt here...",
 
74
  maximum=1.0,
75
  value=0.7,
76
  step=0.1,
77
+ label="Creativity (Temperature)"
78
  )
79
  top_p = gr.Slider(
80
  minimum=0.1,
 
85
  )
86
 
87
  with gr.Column():
88
+ # Output and button
89
  output = gr.Textbox(label="Generated Text", lines=10)
90
+ generate_btn = gr.Button("🚀 Generate", variant="primary")
91
 
92
+ # Event handler
93
  generate_btn.click(
94
  fn=generate_text,
95
  inputs=[prompt, max_tokens, temp, top_p],
96
  outputs=output
97
  )
98
 
99
+ # Launch app
100
  demo.launch()