Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,51 +6,47 @@ import torch
|
|
6 |
tokenizer = AutoTokenizer.from_pretrained("diabolic6045/ELN-Llama-1B-base")
|
7 |
model = AutoModelForCausalLM.from_pretrained("diabolic6045/ELN-Llama-1B-base")
|
8 |
|
9 |
-
def generate_response(message,
|
10 |
-
# Format the conversation history
|
11 |
-
|
12 |
-
print("here")
|
13 |
-
conversation = ""
|
14 |
-
for h in history:
|
15 |
-
conversation += f"User: {h[0]}\nAssistant: {h[1]}\n"
|
16 |
-
conversation += f"User: {message}\nAssistant:"
|
17 |
-
|
18 |
# Tokenize input
|
19 |
-
inputs = tokenizer(
|
20 |
|
21 |
# Generate response
|
22 |
with torch.no_grad():
|
23 |
outputs = model.generate(
|
24 |
inputs["input_ids"],
|
25 |
-
max_length=
|
26 |
-
temperature=
|
27 |
do_sample=True,
|
28 |
pad_token_id=tokenizer.eos_token_id,
|
29 |
num_return_sequences=1,
|
30 |
)
|
31 |
|
32 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
33 |
-
# Extract only the assistant's response
|
34 |
-
response = response.split("Assistant:")[-1].strip()
|
35 |
-
|
36 |
return response
|
37 |
|
38 |
# Create the Gradio interface
|
39 |
-
demo = gr.
|
40 |
fn=generate_response,
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
44 |
examples=[
|
45 |
-
["
|
46 |
-
["
|
47 |
-
["
|
|
|
48 |
],
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
54 |
)
|
55 |
|
56 |
if __name__ == "__main__":
|
|
|
6 |
tokenizer = AutoTokenizer.from_pretrained("diabolic6045/ELN-Llama-1B-base")
|
7 |
model = AutoModelForCausalLM.from_pretrained("diabolic6045/ELN-Llama-1B-base")
|
8 |
|
9 |
+
def generate_response(message, temperature, max_length):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
# Tokenize input
|
11 |
+
inputs = tokenizer(message, return_tensors="pt", truncation=True, max_length=512)
|
12 |
|
13 |
# Generate response
|
14 |
with torch.no_grad():
|
15 |
outputs = model.generate(
|
16 |
inputs["input_ids"],
|
17 |
+
max_length=max_length,
|
18 |
+
temperature=temperature,
|
19 |
do_sample=True,
|
20 |
pad_token_id=tokenizer.eos_token_id,
|
21 |
num_return_sequences=1,
|
22 |
)
|
23 |
|
24 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
25 |
return response
|
26 |
|
27 |
# Create the Gradio interface
|
28 |
+
demo = gr.Interface(
|
29 |
fn=generate_response,
|
30 |
+
inputs=[
|
31 |
+
gr.Textbox(label="Input Text", lines=4, placeholder="Enter your text here and the model will complete it..."),
|
32 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.7, label="Temperature (higher = more creative, lower = more focused)"),
|
33 |
+
gr.Slider(minimum=50, maximum=500, value=200, step=50, label="Max Length (longer text = more completion)"),
|
34 |
+
],
|
35 |
+
outputs=gr.Textbox(label="Generated Completion", lines=4),
|
36 |
+
title="LLaMA Text Completion",
|
37 |
+
description="Generate text completions using the ELN-Llama-1B model. Enter the start of a text, and the model will continue it.",
|
38 |
examples=[
|
39 |
+
["Once upon a time in a magical forest", 0.7, 200],
|
40 |
+
["The recipe for making the perfect chocolate cake requires", 0.7, 200],
|
41 |
+
["In the year 2150, humanity had finally achieved", 0.7, 200],
|
42 |
+
["The most important principles of effective programming are", 0.8, 300],
|
43 |
],
|
44 |
+
article="""
|
45 |
+
## Tips for better completions:
|
46 |
+
- Start with a clear and detailed prompt
|
47 |
+
- Adjust temperature: Higher for creative writing, lower for factual completion
|
48 |
+
- Adjust max length based on how much text you want to generate
|
49 |
+
"""
|
50 |
)
|
51 |
|
52 |
if __name__ == "__main__":
|