Spaces:
No application file
No application file
Create arithmetic_model.py
Browse files- arithmetic_model.py +27 -0
arithmetic_model.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
# Load a pre-trained model for text generation (e.g., GPT-2 or GPT-3)
|
| 4 |
+
model = "gpt2"
|
| 5 |
+
arithmetic_model = pipeline("text-generation", model=model)
|
| 6 |
+
|
| 7 |
+
def perform_arithmetic(operation):
|
| 8 |
+
# Send the arithmetic operation as input to the model
|
| 9 |
+
result = arithmetic_model(operation, max_length=50, num_return_sequences=1)
|
| 10 |
+
return result[0]['generated_text']
|
| 11 |
+
|
| 12 |
+
def main():
|
| 13 |
+
# Example operations
|
| 14 |
+
operations = [
|
| 15 |
+
"What is 5 plus 3?",
|
| 16 |
+
"What is 10 minus 4?",
|
| 17 |
+
"Calculate 15 + 7",
|
| 18 |
+
"Subtract 9 from 20"
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
for operation in operations:
|
| 22 |
+
print(f"Operation: {operation}")
|
| 23 |
+
result = perform_arithmetic(operation)
|
| 24 |
+
print(f"Result: {result}\n")
|
| 25 |
+
|
| 26 |
+
if __name__ == "__main__":
|
| 27 |
+
main()
|