Pisethan commited on
Commit
542b27b
·
verified ·
1 Parent(s): ed5c205

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import os
2
- from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
3
  import gradio as gr
4
 
5
  # Model name and Hugging Face token
@@ -7,30 +7,30 @@ MODEL_NAME = "Pisethan/sangapac-math"
7
  TOKEN = os.getenv("HF_API_TOKEN")
8
 
9
  if not TOKEN:
10
- raise ValueError("Hugging Face API token not found. Set it as an environment variable.")
11
 
12
  # Load model and tokenizer
13
  try:
14
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=TOKEN)
15
- model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, token=TOKEN)
16
- classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
17
  except Exception as e:
18
- classifier = None
19
  print(f"Error loading model or tokenizer: {e}")
20
 
21
  def predict(input_text):
22
- if classifier is None:
23
  return "Model not loaded properly.", {"Error": "Model not loaded properly."}
24
 
25
  try:
26
- result = classifier(input_text)
27
- label = result[0]["label"]
28
- score = result[0]["score"]
29
 
30
- simple_result = f"Category: {label}\nConfidence: {score:.2f}"
31
  detailed_result = {
32
- "Category": label,
33
- "Confidence": score,
34
  }
35
 
36
  return simple_result, detailed_result
@@ -53,7 +53,10 @@ interface = gr.Interface(
53
  gr.JSON(label="Detailed JSON Output"),
54
  ],
55
  title="Sangapac Math Model",
56
- description="A model to classify math problems into categories like Arithmetic, Multiplication, Division, Algebra, and Geometry.",
 
 
 
57
  examples=sample_inputs,
58
  )
59
 
 
1
  import os
2
+ from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
3
  import gradio as gr
4
 
5
  # Model name and Hugging Face token
 
7
  TOKEN = os.getenv("HF_API_TOKEN")
8
 
9
  if not TOKEN:
10
+ raise ValueError("Hugging Face API token not found. Set it as an environment variable (HF_API_TOKEN).")
11
 
12
  # Load model and tokenizer
13
  try:
14
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_auth_token=TOKEN)
15
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME, use_auth_token=TOKEN)
16
+ generator = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
17
  except Exception as e:
18
+ generator = None
19
  print(f"Error loading model or tokenizer: {e}")
20
 
21
  def predict(input_text):
22
+ if generator is None:
23
  return "Model not loaded properly.", {"Error": "Model not loaded properly."}
24
 
25
  try:
26
+ # Generate output
27
+ result = generator(input_text, max_length=256, num_beams=5, early_stopping=True)
28
+ generated_text = result[0]["generated_text"]
29
 
30
+ simple_result = f"Generated Solution:\n{generated_text}"
31
  detailed_result = {
32
+ "Input": input_text,
33
+ "Generated Solution": generated_text,
34
  }
35
 
36
  return simple_result, detailed_result
 
53
  gr.JSON(label="Detailed JSON Output"),
54
  ],
55
  title="Sangapac Math Model",
56
+ description=(
57
+ "A model that solves math problems and provides step-by-step solutions. "
58
+ "Examples include Arithmetic, Multiplication, Division, Algebra, and Geometry problems."
59
+ ),
60
  examples=sample_inputs,
61
  )
62