Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# 1. Initialize the pipeline
|
| 6 |
+
print("Loading Dawn-V1-mini...")
|
| 7 |
+
pipe = pipeline(
|
| 8 |
+
"text-generation",
|
| 9 |
+
model="Dawn-AI/Dawn-V1-mini",
|
| 10 |
+
device_map="auto",
|
| 11 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 12 |
+
)
|
| 13 |
+
print("Model loaded successfully!")
|
| 14 |
+
|
| 15 |
+
# 2. Updated chat function with 'system_prompt'
|
| 16 |
+
def chat_with_dawn(message, history, system_prompt):
|
| 17 |
+
messages = []
|
| 18 |
+
|
| 19 |
+
# Add the system prompt first (if the user provided one)
|
| 20 |
+
if system_prompt.strip():
|
| 21 |
+
messages.append({"role": "system", "content": system_prompt})
|
| 22 |
+
|
| 23 |
+
# Add the conversation history
|
| 24 |
+
for user_msg, assistant_msg in history:
|
| 25 |
+
messages.append({"role": "user", "content": user_msg})
|
| 26 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 27 |
+
|
| 28 |
+
# Add the current user message
|
| 29 |
+
messages.append({"role": "user", "content": message})
|
| 30 |
+
|
| 31 |
+
# Generate the response
|
| 32 |
+
response = pipe(messages, max_new_tokens=512, truncation=True)
|
| 33 |
+
generated_text = response[0]['generated_text']
|
| 34 |
+
|
| 35 |
+
# Extract just the new reply
|
| 36 |
+
if isinstance(generated_text, list):
|
| 37 |
+
return generated_text[-1]['content']
|
| 38 |
+
else:
|
| 39 |
+
return generated_text
|
| 40 |
+
|
| 41 |
+
# 3. Create the System Prompt text box
|
| 42 |
+
system_textbox = gr.Textbox(
|
| 43 |
+
value="You are Dawn V1, a brilliant, logical, and highly capable reasoning AI optimized for coding and complex problem-solving. Always think step-by-step.",
|
| 44 |
+
label="System Prompt",
|
| 45 |
+
lines=3,
|
| 46 |
+
interactive=True
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# 4. Create the Chat Interface and pass the system textbox to 'additional_inputs'
|
| 50 |
+
demo = gr.ChatInterface(
|
| 51 |
+
fn=chat_with_dawn,
|
| 52 |
+
additional_inputs=[system_textbox], # This links the textbox to the third argument of your function
|
| 53 |
+
title="🌅 Dawn V1 Mini Chat",
|
| 54 |
+
description="A lightweight conversational interface powered by Dawn-V1-mini. **Click 'Additional Inputs' below to change Dawn's personality!**",
|
| 55 |
+
examples=["Who are you?", "Write a Python script to reverse a string.", "Explain quantum computing simply."],
|
| 56 |
+
theme="soft",
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# 5. Launch the app
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
demo.launch()
|