willsh1997 commited on
Commit
097ebe4
·
1 Parent(s): d20634b

:sparkles: initial commit - llm calculator

Browse files
.github/workflows/push_to_hub.yml ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Sync to Hugging Face hub
2
+ on:
3
+ push:
4
+ branches: [main]
5
+
6
+ # to run this workflow manually from the Actions tab
7
+ workflow_dispatch:
8
+
9
+ jobs:
10
+ sync-to-hub:
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/checkout@v3
14
+ with:
15
+ fetch-depth: 0
16
+ lfs: true
17
+ - name: Push to hub
18
+ env:
19
+ HF_TOKEN: ${{ secrets.HF_TOKEN }}
20
+ run: git push https://willsh1997:[email protected]/spaces/willsh1997/llm-calculator main
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: llm calculator
3
+ emoji: 🐨
4
+ colorFrom: red
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 5.23.3
8
+ app_file: llmcalc_gradio.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ short_description: llm calculator with llama backend
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
llmcalc_gradio.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+ from transformers import pipeline
5
+ import pandas as pd
6
+ import gradio as gr
7
+
8
+ #Llama 3.2 1b setup
9
+ # quantization_config = BitsAndBytesConfig(load_in_4bit=True)
10
+ torch_device = "cuda" if torch.cuda.is_available() else ("mps" if torch.mps.is_available() else "cpu")
11
+
12
+ torch_dtype = torch.float16 if torch_device in ["cuda", "mps"] else torch.float32
13
+
14
+ llama_model=AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-3B-Instruct",
15
+ # quantization_config=quantization_config,
16
+ torch_dtype=torch_dtype,
17
+ device_map=torch_device,
18
+ load_in_4bit=True) #for puny devices like mine.
19
+
20
+ llama_tokenizer=AutoTokenizer.from_pretrained("meta-llama/Llama-3.2-3B-Instruct")
21
+
22
+
23
+ model_id = "meta-llama/Llama-3.2-3B-Instruct"
24
+ pipe = pipeline(
25
+ "text-generation",
26
+ model=llama_model,
27
+ tokenizer=llama_tokenizer,
28
+ torch_dtype=torch.bfloat16,
29
+ device_map="auto",
30
+ #load_in_4bit = True #for lil machines like minegit statu
31
+ )
32
+
33
+ @spaces.GPU
34
+ def llama_calculator(left_num, operation, right_num):
35
+ """
36
+ stupid func for asking llama a question and then getting an answer
37
+ inputs:
38
+ - input_question [str]: question for llama to answer
39
+ outputs:
40
+ - response [str]: llama's response
41
+ """
42
+
43
+ messages = [
44
+ {"role": "system", "content": """You are a calculator, which generates correct answers to arithmetic questions posed by the user. Answer with ONLY the numerical output of the answer - no text. Answer as accurately as possible, to as many decimal places as required. Here are some examples of questions and answers you may see:
45
+
46
+ USER: 4+8
47
+ CHATBOT: 12
48
+
49
+ ---
50
+
51
+ USER: 182*526
52
+ CHATBOT: 95732
53
+
54
+ ---
55
+
56
+ USER: 97.35/15.02
57
+ CHATBOT: 6.48135818908
58
+
59
+ """},
60
+ {"role": "user", "content": str(left_num)+' '+str(operation)+' '+str(right_num)},
61
+ ]
62
+ outputs = pipe(
63
+ messages,
64
+ max_new_tokens=512
65
+ )
66
+ response = outputs[0]["generated_text"][-1]['content']
67
+ return response
68
+
69
+
70
+ # QA translation roundtrip
71
+
72
+ @spaces.GPU
73
+ def gradio_func(left_num, operation, right_num):
74
+ """
75
+ silly wrapper function for gradio that turns all inputs into a single func. runs both the LHS and RHS of teh 'app' in order to let gradio work correctly.
76
+ """
77
+ left_output = llama_calculator(left_num, operation, right_num)
78
+ right_output = eval(str(left_num)+' '+str(operation)+' '+str(right_num))
79
+ return left_output, right_output
80
+
81
+ # Create the Gradio interface
82
+ def create_interface():
83
+ # Get available languages from the flores_dict
84
+
85
+ with gr.Blocks() as demo:
86
+ with gr.Row():
87
+ left_num = gr.Number(label="left number", interactive=True)
88
+ operation = gr.Dropdown(["+","-","*","/"], multiselect = False)
89
+ right_num = gr.Number(label="right number", interactive=True)
90
+ with gr.Row():
91
+ submit_btn = gr.Button("calculate")
92
+ with gr.Row():
93
+ left_output = gr.Textbox(label="LLM output", interactive=False)
94
+ right_output = gr.Textbox(label="actual calculated answer", interactive=False)
95
+
96
+ submit_btn.click(
97
+ fn=gradio_func,
98
+ inputs=[left_num, operation, right_num],
99
+ outputs=[left_output, right_output]
100
+ )
101
+
102
+ return demo
103
+
104
+ # Launch the app
105
+ demo = create_interface()
106
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ accelerate==1.4.0
2
+ aiofiles==23.2.1
3
+ annotated-types==0.7.0
4
+ anyio==4.8.0
5
+ asttokens==3.0.0
6
+ bitsandbytes==0.45.4
7
+ certifi==2025.1.31
8
+ charset-normalizer==3.4.1
9
+ click==8.1.8
10
+ comm==0.2.2
11
+ debugpy==1.8.12
12
+ decorator==5.1.1
13
+ exceptiongroup==1.2.2
14
+ executing==2.2.0
15
+ fastapi==0.115.8
16
+ ffmpy==0.5.0
17
+ filelock==3.17.0
18
+ fsspec==2025.2.0
19
+ gradio==5.16.1
20
+ gradio_client==1.7.0
21
+ h11==0.14.0
22
+ httpcore==1.0.7
23
+ httpx==0.28.1
24
+ huggingface-hub==0.28.1
25
+ idna==3.10
26
+ ipykernel==6.29.5
27
+ ipython==8.32.0
28
+ jedi==0.19.2
29
+ Jinja2==3.1.5
30
+ jupyter_client==8.6.3
31
+ jupyter_core==5.7.2
32
+ markdown-it-py==3.0.0
33
+ MarkupSafe==2.1.5
34
+ matplotlib-inline==0.1.7
35
+ mdurl==0.1.2
36
+ mpmath==1.3.0
37
+ nest-asyncio==1.6.0
38
+ networkx==3.4.2
39
+ numpy==2.2.3
40
+ orjson==3.10.15
41
+ packaging==24.2
42
+ pandas==2.2.3
43
+ parso==0.8.4
44
+ pexpect==4.9.0
45
+ pillow==11.1.0
46
+ platformdirs==4.3.6
47
+ prompt_toolkit==3.0.50
48
+ psutil==7.0.0
49
+ ptyprocess==0.7.0
50
+ pure_eval==0.2.3
51
+ pydantic==2.10.6
52
+ pydantic_core==2.27.2
53
+ pydub==0.25.1
54
+ Pygments==2.19.1
55
+ python-dateutil==2.9.0.post0
56
+ python-multipart==0.0.20
57
+ pytz==2025.1
58
+ PyYAML==6.0.2
59
+ pyzmq==26.2.1
60
+ regex==2024.11.6
61
+ requests==2.32.3
62
+ rich==13.9.4
63
+ ruff==0.9.6
64
+ safehttpx==0.1.6
65
+ safetensors==0.5.2
66
+ semantic-version==2.10.0
67
+ shellingham==1.5.4
68
+ six==1.17.0
69
+ sniffio==1.3.1
70
+ stack-data==0.6.3
71
+ starlette==0.45.3
72
+ sympy==1.13.1
73
+ tokenizers==0.21.0
74
+ tomlkit==0.13.2
75
+ torch==2.4.0
76
+ tornado==6.4.2
77
+ tqdm==4.67.1
78
+ traitlets==5.14.3
79
+ transformers==4.49.0
80
+ typer==0.15.1
81
+ typing_extensions==4.12.2
82
+ tzdata==2025.1
83
+ urllib3==2.3.0
84
+ uvicorn==0.34.0
85
+ wcwidth==0.2.13
86
+ websockets==14.2
87
+