chendren commited on
Commit
86979a0
·
verified ·
1 Parent(s): 66d9f03

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +181 -0
app.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import requests
4
+ import os
5
+ import time
6
+
7
+ # Model configuration
8
+ MODEL_ID = "chendren/deepseek-dnd-lora"
9
+ API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
10
+ API_TOKEN = os.environ.get("HUGGINGFACE_TOKEN", "")
11
+
12
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
13
+
14
+ # Example prompts
15
+ example_prompts = [
16
+ "Create a D&D character with the following details: Race: Half-Elf, Class: Bard, Background: Entertainer",
17
+ "Design a D&D adventure hook set in a dark forest with a mysterious cult",
18
+ "Create a magical item for D&D 5e that would be suitable for a level 5 rogue",
19
+ "Write a description for a fantasy tavern in a D&D setting called 'The Dragon\'s Rest'",
20
+ "Create a D&D monster stat block for a new type of undead creature called 'Mist Wraith'",
21
+ "Write a backstory for a Dragonborn Paladin who follows the Oath of Vengeance",
22
+ ]
23
+
24
+ def generate_text(prompt, temperature=0.7, max_length=500, top_p=0.9, top_k=50, rep_penalty=1.1):
25
+ """
26
+ Generate text using the Hugging Face Inference API
27
+ If the API fails, provide instructions for manual testing
28
+ """
29
+ full_prompt = f"You are a Dungeons & Dragons assistant. {prompt}"
30
+
31
+ payload = {
32
+ "inputs": full_prompt,
33
+ "parameters": {
34
+ "max_new_tokens": int(max_length),
35
+ "temperature": float(temperature),
36
+ "top_p": float(top_p),
37
+ "top_k": int(top_k),
38
+ "repetition_penalty": float(rep_penalty),
39
+ "do_sample": True
40
+ }
41
+ }
42
+
43
+ try:
44
+ response = requests.post(API_URL, headers=headers, json=payload)
45
+
46
+ if response.status_code == 200:
47
+ result = response.json()
48
+ if isinstance(result, list) and len(result) > 0:
49
+ if "generated_text" in result[0]:
50
+ return result[0]["generated_text"]
51
+ else:
52
+ return str(result[0])
53
+ else:
54
+ return str(result)
55
+
56
+ elif response.status_code == 503:
57
+ return (
58
+ "⏳ The model is still loading or being processed by Hugging Face. "
59
+ "This is normal for new or infrequently used models. Please try again in a few minutes.\n\n"
60
+ "If the problem persists, you can also try using the model directly via code: "
61
+ "https://huggingface.co/chendren/deepseek-dnd-lora#using-the-transformers-library"
62
+ )
63
+
64
+ else:
65
+ return (
66
+ f"⚠️ Error: API returned status code {response.status_code}\n\n"
67
+ f"Response: {response.text}\n\n"
68
+ "You can try again later or use the model directly with the code provided here: "
69
+ "https://huggingface.co/chendren/deepseek-dnd-lora"
70
+ )
71
+
72
+ except Exception as e:
73
+ return (
74
+ f"⚠️ An error occurred: {str(e)}\n\n"
75
+ "Check if the model is available or try using the model directly using the code examples: "
76
+ "https://huggingface.co/chendren/deepseek-dnd-lora"
77
+ )
78
+
79
+ # Create the Gradio interface
80
+ with gr.Blocks(title="D&D Content Generator", css="footer {visibility: hidden}") as demo:
81
+ gr.Markdown(
82
+ """
83
+ # 🐉 Dungeons & Dragons Content Generator
84
+
85
+ This demo uses the [DeepSeek-R1-Distill-Qwen-7B](https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-7B) model fine-tuned on D&D content.
86
+
87
+ Enter a prompt related to D&D content creation, or try one of the examples below.
88
+
89
+ ### Model Details
90
+ - Base model: [deepseek-ai/DeepSeek-R1-Distill-Qwen-7B](https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-7B)
91
+ - Fine-tuned with LoRA on 500 examples of D&D content
92
+ - Specialized for creating characters, adventures, items, and more for D&D
93
+
94
+ [View Model on Hugging Face](https://huggingface.co/chendren/deepseek-dnd-lora)
95
+ """
96
+ )
97
+
98
+ with gr.Row():
99
+ with gr.Column():
100
+ prompt = gr.Textbox(
101
+ label="Prompt",
102
+ placeholder="Enter your D&D content request...",
103
+ lines=4
104
+ )
105
+
106
+ with gr.Row():
107
+ submit_btn = gr.Button("Generate", variant="primary")
108
+ clear_btn = gr.Button("Clear")
109
+
110
+ with gr.Accordion("Advanced Parameters", open=False):
111
+ temperature = gr.Slider(
112
+ minimum=0.1, maximum=1.5, value=0.7, step=0.1,
113
+ label="Temperature (creativity)",
114
+ info="Higher values produce more creative but potentially less coherent output"
115
+ )
116
+ max_length = gr.Slider(
117
+ minimum=100, maximum=1000, value=500, step=50,
118
+ label="Maximum Length",
119
+ info="Maximum number of tokens to generate"
120
+ )
121
+ top_p = gr.Slider(
122
+ minimum=0.1, maximum=1.0, value=0.9, step=0.1,
123
+ label="Top-p",
124
+ info="Nucleus sampling probability threshold"
125
+ )
126
+ top_k = gr.Slider(
127
+ minimum=1, maximum=100, value=50, step=5,
128
+ label="Top-k",
129
+ info="Number of highest probability tokens to consider"
130
+ )
131
+ rep_penalty = gr.Slider(
132
+ minimum=1.0, maximum=2.0, value=1.1, step=0.1,
133
+ label="Repetition Penalty",
134
+ info="Penalizes repeated tokens (higher = less repetition)"
135
+ )
136
+
137
+ with gr.Column():
138
+ output = gr.Textbox(
139
+ label="Generated Output",
140
+ placeholder="D&D content will appear here...",
141
+ lines=20
142
+ )
143
+
144
+ gr.Examples(
145
+ examples=example_prompts,
146
+ inputs=prompt,
147
+ outputs=output,
148
+ fn=lambda x: generate_text(x),
149
+ cache_examples=False
150
+ )
151
+
152
+ gr.Markdown(
153
+ """
154
+ ### Usage Tips
155
+ - Be specific in your prompts, including details like race, class, and level where applicable
156
+ - For character creation, specify race, class, background, and any other details
157
+ - For adventure hooks, specify setting, theme, and target party level
158
+ - For magic items, specify item type, rarity, and intended user
159
+
160
+ ### Credits
161
+ - Base model: [DeepSeek-R1-Distill-Qwen-7B](https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-7B)
162
+ - Fine-tuned by: [chendren](https://huggingface.co/chendren)
163
+ """
164
+ )
165
+
166
+ # Set up event handlers
167
+ submit_btn.click(
168
+ fn=generate_text,
169
+ inputs=[prompt, temperature, max_length, top_p, top_k, rep_penalty],
170
+ outputs=output
171
+ )
172
+
173
+ clear_btn.click(
174
+ fn=lambda: ("", ""),
175
+ inputs=[],
176
+ outputs=[prompt, output]
177
+ )
178
+
179
+ # Launch the app
180
+ if __name__ == "__main__":
181
+ demo.launch()