Update app.py
Browse files
app.py
CHANGED
@@ -1,167 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import spaces
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
description = """
|
8 |
-
This demo uses Microsoft's Phi-4 model for text generation.
|
9 |
-
- System Prompt: Sets the context/role for the AI
|
10 |
-
- User Prompt: Your specific question or request
|
11 |
-
- Max Tokens: Maximum length of the generated response
|
12 |
-
- Temperature: Controls randomness (higher = more creative, lower = more focused)
|
13 |
-
"""
|
14 |
-
|
15 |
-
|
16 |
-
join_us = """
|
17 |
-
## Join us:
|
18 |
-
🌟TeamTonic🌟 is always making cool demos! Join our active builder's 🛠️community 👻
|
19 |
-
[](https://discord.gg/qdfnvSPcqP)
|
20 |
-
On 🤗Huggingface: [MultiTransformer](https://huggingface.co/MultiTransformer)
|
21 |
-
On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to🌟 [Dark Thoughts](https://github.com/MultiTonic/thinking-dataset)
|
22 |
-
🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
|
23 |
-
"""
|
24 |
-
|
25 |
-
@spaces.GPU(duration=180)
|
26 |
-
def generate_response(system_prompt, user_prompt, max_tokens, temperature):
|
27 |
-
pipeline = transformers.pipeline(
|
28 |
-
"text-generation",
|
29 |
-
model="microsoft/phi-4",
|
30 |
-
model_kwargs={"torch_dtype": "auto"},
|
31 |
-
device_map="auto",
|
32 |
-
)
|
33 |
-
|
34 |
-
messages = [
|
35 |
-
{"role": "system", "content": system_prompt},
|
36 |
-
{"role": "user", "content": user_prompt},
|
37 |
-
]
|
38 |
-
|
39 |
-
outputs = pipeline(
|
40 |
-
messages,
|
41 |
-
max_new_tokens=max_tokens,
|
42 |
-
temperature=temperature,
|
43 |
-
do_sample=True
|
44 |
-
)
|
45 |
-
|
46 |
-
# Parse the response to get only the assistant's message
|
47 |
-
try:
|
48 |
-
response_text = outputs[0]["generated_text"]
|
49 |
-
# Find the assistant's content
|
50 |
-
assistant_start = response_text.find("'assistant', 'content': '") + len("'assistant', 'content': '")
|
51 |
-
assistant_end = response_text.rfind("'}")
|
52 |
-
if assistant_start > -1 and assistant_end > -1:
|
53 |
-
return response_text[assistant_start:assistant_end]
|
54 |
-
else:
|
55 |
-
return "Could not parse response properly"
|
56 |
-
except Exception as e:
|
57 |
-
return f"Error parsing response: {str(e)}"
|
58 |
-
|
59 |
-
# Example configurations
|
60 |
-
examples = [
|
61 |
-
[
|
62 |
-
"You are a medieval knight and must provide explanations to modern people.",
|
63 |
-
"How should I explain the Internet?",
|
64 |
-
128,
|
65 |
-
0.7
|
66 |
-
],
|
67 |
-
[
|
68 |
-
"You are a wise wizard from ancient times.",
|
69 |
-
"What would you call a smartphone?",
|
70 |
-
256,
|
71 |
-
0.8
|
72 |
-
],
|
73 |
-
[
|
74 |
-
"You are a time-traveling merchant from the year 1400.",
|
75 |
-
"How would you describe modern cars?",
|
76 |
-
200,
|
77 |
-
0.6
|
78 |
-
],
|
79 |
-
[
|
80 |
-
"You are a medieval monk who specializes in manuscripts.",
|
81 |
-
"What do you think about e-books?",
|
82 |
-
150,
|
83 |
-
0.7
|
84 |
-
],
|
85 |
-
[
|
86 |
-
"You are a castle guard from the Middle Ages.",
|
87 |
-
"What do you think about modern security systems?",
|
88 |
-
180,
|
89 |
-
0.9
|
90 |
-
]
|
91 |
-
]
|
92 |
-
|
93 |
-
# Create the Gradio interface
|
94 |
-
with gr.Blocks() as demo:
|
95 |
-
gr.Markdown(title)
|
96 |
-
gr.Markdown(description)
|
97 |
-
gr.Markdown(join_us)
|
98 |
-
|
99 |
-
with gr.Row():
|
100 |
-
with gr.Column():
|
101 |
-
system_prompt = gr.Textbox(
|
102 |
-
label="🤖Instructions",
|
103 |
-
placeholder="Enter system prompt...",
|
104 |
-
value="You are a medieval knight and must provide explanations to modern people."
|
105 |
-
)
|
106 |
-
user_prompt = gr.Textbox(
|
107 |
-
label="🗣️User Message",
|
108 |
-
placeholder="Enter your question...",
|
109 |
-
value="How should I explain the Internet?"
|
110 |
-
)
|
111 |
-
|
112 |
-
with gr.Row():
|
113 |
-
max_tokens = gr.Slider(
|
114 |
-
minimum=1,
|
115 |
-
maximum=512,
|
116 |
-
value=128,
|
117 |
-
step=1,
|
118 |
-
label="🪙Maximum Tokens"
|
119 |
-
)
|
120 |
-
temperature = gr.Slider(
|
121 |
-
minimum=0.1,
|
122 |
-
maximum=1.0,
|
123 |
-
value=0.7,
|
124 |
-
step=0.1,
|
125 |
-
label="🌡️Temperature"
|
126 |
-
)
|
127 |
-
|
128 |
-
submit_btn = gr.Button("🚀 Generate Response")
|
129 |
-
|
130 |
-
with gr.Column():
|
131 |
-
output = gr.Textbox(
|
132 |
-
label="🤳🏻Phi-4",
|
133 |
-
lines=10
|
134 |
-
)
|
135 |
-
|
136 |
-
gr.Examples(
|
137 |
-
examples=examples,
|
138 |
-
inputs=[system_prompt, user_prompt, max_tokens, temperature],
|
139 |
-
outputs=output,
|
140 |
-
fn=generate_response,
|
141 |
-
cache_examples=False,
|
142 |
-
label="Example Prompts"
|
143 |
-
)
|
144 |
-
|
145 |
-
submit_btn.click(
|
146 |
-
fn=generate_response,
|
147 |
-
inputs=[system_prompt, user_prompt, max_tokens, temperature],
|
148 |
-
outputs=output
|
149 |
-
)
|
150 |
-
|
151 |
-
gr.Markdown("""
|
152 |
-
### 📝 Parameters:
|
153 |
-
- **System Prompt**: Sets the behavior/role of the AI (e.g., medieval knight, wizard, merchant)
|
154 |
-
- **User Prompt**: Your question or input about modern concepts
|
155 |
-
- **Maximum Tokens**: Controls the maximum length of the generated response
|
156 |
-
- **Temperature**: Controls randomness (higher = more creative, lower = more focused)
|
157 |
-
|
158 |
-
### 💡 Tips:
|
159 |
-
1. Try different historical personas in the system prompt
|
160 |
-
2. Ask about modern technology from a historical perspective
|
161 |
-
3. Adjust temperature for more varied or consistent responses
|
162 |
-
4. Use the examples below for inspiration
|
163 |
-
""")
|
164 |
-
|
165 |
-
# Launch the demo
|
166 |
-
if __name__ == "__main__":
|
167 |
-
demo.launch(ssr_mode=False)
|
|
|
1 |
import gradio as gr
|
2 |
+
import ai_gradio
|
|
|
3 |
|
4 |
+
gr.load(
|
5 |
+
name='transformers:phi-4', src=ai_gradio.registry).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|