Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -9,46 +9,37 @@ tokenizer = AutoTokenizer.from_pretrained(model_name, token="hf_tjRQQxpOvAuXkssS
|
|
9 |
model = AutoModelForCausalLM.from_pretrained(model_name, token="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")
|
10 |
|
11 |
# System prompt
|
12 |
-
system_prompt = """You are
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
def little_thinking(prompt):
|
15 |
thoughts = []
|
16 |
for i in range(4):
|
17 |
-
thought = f"*Berry-{i+1}: "
|
18 |
-
thought += generate_response(f"As Berry-{i+1}, think about this query: {prompt}")
|
19 |
-
thought += "*\n\n"
|
20 |
thoughts.append(thought)
|
21 |
return "".join(thoughts)
|
22 |
|
23 |
def reviewer_thinking(prompt):
|
24 |
-
|
25 |
-
review += generate_response(f"As a Reviewer, carefully check this answer: {prompt}")
|
26 |
-
review += "*\n\n"
|
27 |
-
return review
|
28 |
|
29 |
def second_reviewer_thinking(prompt):
|
30 |
-
|
31 |
-
review += generate_response(f"As a Second Reviewer, think deeper about physics, coordination, and science to verify this answer: {prompt}")
|
32 |
-
review += "*\n\n"
|
33 |
-
return review
|
34 |
-
|
35 |
-
def generate_response(prompt):
|
36 |
-
full_prompt = f"{system_prompt}\n\nUser: {prompt}\n\nBlackBerry:"
|
37 |
-
inputs = tokenizer(full_prompt, return_tensors="pt")
|
38 |
-
with torch.no_grad():
|
39 |
-
outputs = model.generate(**inputs, max_length=500, num_return_sequences=1, temperature=0.7)
|
40 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
41 |
-
return response.split("BlackBerry:")[-1].strip()
|
42 |
|
43 |
def blackberry_response(prompt):
|
44 |
-
response = "BlackBerry:
|
45 |
|
46 |
# Little Thinking process
|
47 |
response += little_thinking(prompt)
|
48 |
|
49 |
-
#
|
50 |
-
response += "BlackBerry:
|
51 |
-
response += generate_response(prompt) + "\n\n"
|
52 |
|
53 |
# Reviewer
|
54 |
response += reviewer_thinking(response)
|
@@ -58,8 +49,7 @@ def blackberry_response(prompt):
|
|
58 |
response += second_reviewer_thinking(response)
|
59 |
|
60 |
# Final answer
|
61 |
-
response += "BlackBerry:
|
62 |
-
response += generate_response(prompt)
|
63 |
|
64 |
return response
|
65 |
|
@@ -69,8 +59,8 @@ iface = gr.Interface(
|
|
69 |
inputs=gr.Textbox(lines=5, label="Enter your query"),
|
70 |
outputs=gr.Textbox(label="BlackBerry's Response"),
|
71 |
title="Blackberry-1 LLM",
|
72 |
-
description="Powered by
|
73 |
)
|
74 |
|
75 |
# Launch the app
|
76 |
-
iface.launch()
|
|
|
9 |
model = AutoModelForCausalLM.from_pretrained(model_name, token="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")
|
10 |
|
11 |
# System prompt
|
12 |
+
system_prompt = """You are BlackBerry, an advanced AI model with the "Little Thinking" technique. You use four "Berry" thinkers to analyze queries and provide accurate responses."""
|
13 |
+
|
14 |
+
def generate_response(prompt, max_length=100):
|
15 |
+
full_prompt = f"{system_prompt}\n\nUser: {prompt}\n\nBlackBerry:"
|
16 |
+
inputs = tokenizer(full_prompt, return_tensors="pt", truncation=True, max_length=512)
|
17 |
+
with torch.no_grad():
|
18 |
+
outputs = model.generate(**inputs, max_length=max_length, num_return_sequences=1, temperature=0.7)
|
19 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
20 |
+
return response.split("BlackBerry:")[-1].strip()
|
21 |
|
22 |
def little_thinking(prompt):
|
23 |
thoughts = []
|
24 |
for i in range(4):
|
25 |
+
thought = f"*Berry-{i+1}: {generate_response(f'As Berry-{i+1}, briefly analyze: {prompt}', max_length=50)}*\n\n"
|
|
|
|
|
26 |
thoughts.append(thought)
|
27 |
return "".join(thoughts)
|
28 |
|
29 |
def reviewer_thinking(prompt):
|
30 |
+
return f"*Reviewer: {generate_response(f'As a Reviewer, briefly check: {prompt}', max_length=50)}*\n\n"
|
|
|
|
|
|
|
31 |
|
32 |
def second_reviewer_thinking(prompt):
|
33 |
+
return f"*Second Reviewer: {generate_response(f'As a Second Reviewer, briefly verify: {prompt}', max_length=50)}*\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
def blackberry_response(prompt):
|
36 |
+
response = "BlackBerry: Analyzing with Little Thinking technique.\n\n"
|
37 |
|
38 |
# Little Thinking process
|
39 |
response += little_thinking(prompt)
|
40 |
|
41 |
+
# Initial answer
|
42 |
+
response += f"BlackBerry: Initial answer:\n{generate_response(prompt, max_length=100)}\n\n"
|
|
|
43 |
|
44 |
# Reviewer
|
45 |
response += reviewer_thinking(response)
|
|
|
49 |
response += second_reviewer_thinking(response)
|
50 |
|
51 |
# Final answer
|
52 |
+
response += f"BlackBerry: Final answer:\n{generate_response(prompt, max_length=150)}"
|
|
|
53 |
|
54 |
return response
|
55 |
|
|
|
59 |
inputs=gr.Textbox(lines=5, label="Enter your query"),
|
60 |
outputs=gr.Textbox(label="BlackBerry's Response"),
|
61 |
title="Blackberry-1 LLM",
|
62 |
+
description="Powered by meta-llama/Llama-3.2-1B with 'Little Thinking' technique"
|
63 |
)
|
64 |
|
65 |
# Launch the app
|
66 |
+
iface.launch()
|