Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load tokenizer and model (simulating EvoTransformer with GPT-2-like architecture)
|
6 |
+
model_name = "gpt2" # Replace with fine-tuned EvoTransformer model on Hugging Face if available
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
+
model.eval()
|
10 |
+
|
11 |
+
# Mock EvoTransformer architecture traits
|
12 |
+
architecture = {
|
13 |
+
"layers": 6,
|
14 |
+
"heads": 8,
|
15 |
+
"ffn_dim": 2048,
|
16 |
+
"parameters": "58M"
|
17 |
+
}
|
18 |
+
|
19 |
+
def generate_response(user_input, max_length=100):
|
20 |
+
# Tokenize input with a conversational prompt
|
21 |
+
prompt = f"User: {user_input} Assistant: "
|
22 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
23 |
+
input_ids = inputs["input_ids"]
|
24 |
+
|
25 |
+
# Generate response
|
26 |
+
with torch.no_grad():
|
27 |
+
outputs = model.generate(
|
28 |
+
input_ids,
|
29 |
+
max_length=max_length,
|
30 |
+
num_return_sequences=1,
|
31 |
+
do_sample=True,
|
32 |
+
top_p=0.9,
|
33 |
+
temperature=0.7,
|
34 |
+
pad_token_id=tokenizer.eos_token_id
|
35 |
+
)
|
36 |
+
|
37 |
+
# Decode response
|
38 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
39 |
+
response = response[len(prompt):].strip()
|
40 |
+
|
41 |
+
# Format output with architecture details
|
42 |
+
arch_info = (
|
43 |
+
f"Model Architecture:\n"
|
44 |
+
f"- Layers: {architecture['layers']}\n"
|
45 |
+
f"- Attention Heads: {architecture['heads']}\n"
|
46 |
+
f"- FFN Dimension: {architecture['ffn_dim']}\n"
|
47 |
+
f"- Parameters: {architecture['parameters']}"
|
48 |
+
)
|
49 |
+
|
50 |
+
return f"**Response**: {response}\n\n**{arch_info}**"
|
51 |
+
|
52 |
+
# Gradio interface
|
53 |
+
iface = gr.Interface(
|
54 |
+
fn=generate_response,
|
55 |
+
inputs=gr.Textbox(lines=2, placeholder="Type your message here..."),
|
56 |
+
outputs="markdown",
|
57 |
+
title="EvoTransformer Chat Demo",
|
58 |
+
description="Chat with a simplified EvoTransformer model, designed to evolve Transformer architectures. Enter a message to get a response and view model details."
|
59 |
+
)
|
60 |
+
|
61 |
+
if __name__ == "__main__":
|
62 |
+
iface.launch()
|