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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math # For access to infinity
|
| 2 |
+
|
| 3 |
+
import gradio # For building the interface
|
| 4 |
+
import pandas # For working with tables
|
| 5 |
+
|
| 6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline # For LLMS
|
| 7 |
+
|
| 8 |
+
# Instantiate the model that we'll be calling. This is a tiny one!
|
| 9 |
+
MODEL_ID = "HuggingFaceTB/SmolLM2-135M-Instruct"
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 11 |
+
pipe = pipeline(
|
| 12 |
+
task="text-generation",
|
| 13 |
+
model=AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
MODEL_ID,
|
| 15 |
+
),
|
| 16 |
+
tokenizer=tokenizer
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Create a function to calculate the Drake Equation
|
| 20 |
+
def drake_equation(R: float, fp: float, ne: float, fl: float, fi: float, fc: float, L: float) -> float:
|
| 21 |
+
"""
|
| 22 |
+
Calculate the Drake Equation
|
| 23 |
+
R is the rate at which stars are born
|
| 24 |
+
fp is the fraction of stars that host planets
|
| 25 |
+
ne is the number of habitable planets per planetary system
|
| 26 |
+
fl is the fraction of those planets where life occurs
|
| 27 |
+
fi is the fraction of life that evolves intelligence
|
| 28 |
+
fc is the fraction of intelligent life that develops communication capabilities
|
| 29 |
+
L is the average length of time civilizations are detectable
|
| 30 |
+
Return N - the number of civilizations in our galaxy with which communication might be possible
|
| 31 |
+
"""
|
| 32 |
+
N = R * fp * ne * fl * fi * fc * L
|
| 33 |
+
|
| 34 |
+
return dict(
|
| 35 |
+
results={
|
| 36 |
+
"N" : N,
|
| 37 |
+
},
|
| 38 |
+
verdict={
|
| 39 |
+
"Based on your input, the number of alien civilizations that communication may be possible with is": N,
|
| 40 |
+
}
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# This helper function applies a chat format to help the LLM understand what is going on
|
| 44 |
+
def _format_chat(system_prompt: str, user_prompt: str) -> str:
|
| 45 |
+
messages = [
|
| 46 |
+
{"role": "system", "content": system_prompt},
|
| 47 |
+
{"role": "user", "content": user_prompt},
|
| 48 |
+
]
|
| 49 |
+
template = getattr(tokenizer, "chat_template", None)
|
| 50 |
+
return tokenizer.apply_chat_template(
|
| 51 |
+
messages,
|
| 52 |
+
tokenize=False,
|
| 53 |
+
add_generation_prompt=True
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# This functoin uses the LLM to generate a response.
|
| 57 |
+
def _llm_generate(prompt: str, max_tokens: int) -> str:
|
| 58 |
+
out = pipe(
|
| 59 |
+
prompt,
|
| 60 |
+
max_new_tokens=max_tokens,
|
| 61 |
+
do_sample=True,
|
| 62 |
+
temperature=0.5,
|
| 63 |
+
return_full_text=False,
|
| 64 |
+
)
|
| 65 |
+
return out[0]["generated_text"]
|
| 66 |
+
|
| 67 |
+
# This function generates an explanation of the results
|
| 68 |
+
def llm_explain(results: dict, inputs: list) -> str:
|
| 69 |
+
R, fp, ne, fl, fi, fc, L = inputs
|
| 70 |
+
r = results["results"]
|
| 71 |
+
v = results["verdict"]
|
| 72 |
+
|
| 73 |
+
system_prompt = (
|
| 74 |
+
"You explain the implications of Drake Equation calculation to a smart college student."
|
| 75 |
+
"You comment on the implication of their results and how many or few extraterrestrial civilizations are identified."
|
| 76 |
+
"You always return CONCISE responses, only one sentence."
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
user_prompt = (
|
| 80 |
+
f"The rate at which stars are born is {R}.\n"
|
| 81 |
+
f"The fraction of stars that host planets is {fp}.\n"
|
| 82 |
+
f"The number of habitable planets per planetary system is {ne}.\n "
|
| 83 |
+
f"The fraction of those planets where life occurs is {fl}.\n"
|
| 84 |
+
f"The fraction of life that evolves intelligence is {fi}.\n"
|
| 85 |
+
f"The fraction of intelligent life that develops communication capabilities is {fc}.\n"
|
| 86 |
+
f"The average length of time civilizations are detectable is {L}.\n"
|
| 87 |
+
f"The number of civilizations in our galaxy with which communication may be possible is {r['N']}.\n"
|
| 88 |
+
"Explain the results of this Drake Equation calculation in ONE friendly sentence for a non-expert"
|
| 89 |
+
""
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
formatted = _format_chat(system_prompt, user_prompt)
|
| 93 |
+
return _llm_generate(formatted, max_tokens=128)
|
| 94 |
+
|
| 95 |
+
# This function ties everythign together (evaluation, LLM explanaation, output)
|
| 96 |
+
# And will be out main entry point for teh GUI
|
| 97 |
+
def run_once(R, fp, ne, fl, fi, fc, L):
|
| 98 |
+
inputs = [R, fp, ne, fl, fi, fc, L]
|
| 99 |
+
d = drake_equation(
|
| 100 |
+
R=float(R),
|
| 101 |
+
fp=float(fp),
|
| 102 |
+
ne=float(ne),
|
| 103 |
+
fl=float(fl),
|
| 104 |
+
fi=float(fi),
|
| 105 |
+
fc=float(fc),
|
| 106 |
+
L=float(L),
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
df = pandas.DataFrame([{
|
| 110 |
+
"The number of civilizations in our galaxy with which communication may be possible": round(d["results"]["N"], 3),
|
| 111 |
+
}])
|
| 112 |
+
|
| 113 |
+
narrative = llm_explain(d, inputs).split("\n")[0]
|
| 114 |
+
return df, narrative
|
| 115 |
+
|
| 116 |
+
# Last but not least, here's the UI!
|
| 117 |
+
with gradio.Blocks() as demo:
|
| 118 |
+
|
| 119 |
+
# Let's start by adding a title and introduction
|
| 120 |
+
gradio.Markdown(
|
| 121 |
+
"# Run and Explain the Drake Equation"
|
| 122 |
+
)
|
| 123 |
+
gradio.Markdown(
|
| 124 |
+
"This app runs the Drake Equation calculation for estimating extraterrestrial life and returns a natural language description of the results"
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
# This row contains all of the physical parameters
|
| 128 |
+
with gradio.Row():
|
| 129 |
+
R = gradio.Number(value=0.0, label="Rate at which stars are born [stars per year]")
|
| 130 |
+
fp = gradio.Number(value=0.0, label="Fraction of stars that host planets")
|
| 131 |
+
ne = gradio.Number(value=0.0, label="Number of habitable planets per planetary system")
|
| 132 |
+
fl = gradio.Number(value=0.0, label="Fraction of those planets where life occurs")
|
| 133 |
+
fi = gradio.Number(value=0.0, label="Fraction of life that evolves intelligence")
|
| 134 |
+
fc = gradio.Number(value=0.0, label="Fraction of intelligent life that develops communication capabilities")
|
| 135 |
+
L = gradio.Number(value=0.0, label="Average length of time civilizations are detectable [years]")
|
| 136 |
+
|
| 137 |
+
# Add a button to click to run the interface
|
| 138 |
+
run_btn = gradio.Button("Compute")
|
| 139 |
+
|
| 140 |
+
# These are the outputs. We use both a dataframe (for tabular info) and a markdown box
|
| 141 |
+
# for info from teh LLM
|
| 142 |
+
results_df = gradio.Dataframe(label="Numerical results (deterministic)", interactive=False)
|
| 143 |
+
explain_md = gradio.Markdown(label="Explanation")
|
| 144 |
+
|
| 145 |
+
# Run the calculations when the button is clicked
|
| 146 |
+
run_btn.click(fn=run_once, inputs=[R, fp, ne, fl, fi, fc, L], outputs=[results_df, explain_md])
|
| 147 |
+
|
| 148 |
+
# Finally, add a few examples
|
| 149 |
+
gradio.Examples(
|
| 150 |
+
examples=[
|
| 151 |
+
[3.0, 0.95, 5.0, 0.01, 0.01, 0.5, 100000.0],
|
| 152 |
+
[1.5, 0.75, 3.0, 0.0001, 0.001, 0.01, 10000.0],
|
| 153 |
+
[4.0, 0.9, 6.5, 0.000005, 0.05, 0.2, 5000000.0],
|
| 154 |
+
],
|
| 155 |
+
inputs=[R, fp, ne, fl, fi, fc, L],
|
| 156 |
+
label="Representative cases",
|
| 157 |
+
examples_per_page=3,
|
| 158 |
+
cache_examples=False,
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
if __name__ == "__main__":
|
| 162 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pandas
|
| 3 |
+
transformers
|