Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,58 +4,52 @@ import os
|
|
| 4 |
|
| 5 |
# Define models
|
| 6 |
MODEL_OPTIONS = {
|
| 7 |
-
"Gemma 3-4B": "google/gemma-3-4b-it",
|
| 8 |
-
"Mistral 7B": "mistralai/mistral-7b-instruct",
|
| 9 |
-
"Qwen 0.6B": "qwen/qwen3-0.6b-04-28",
|
| 10 |
-
"DeepSeek 70B": "deepseek/deepseek-r1-distill-llama-70b"
|
| 11 |
}
|
| 12 |
|
| 13 |
EVALUATION_MODEL = "qwen/qwen3-32b"
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
|
| 18 |
# API query function
|
| 19 |
-
def
|
| 20 |
headers = {
|
| 21 |
-
"Authorization": f"Bearer {
|
| 22 |
"Content-Type": "application/json"
|
| 23 |
}
|
| 24 |
|
| 25 |
payload = {
|
| 26 |
-
"
|
| 27 |
-
"
|
| 28 |
-
"
|
| 29 |
-
|
| 30 |
-
"temperature": 0.7
|
| 31 |
-
}
|
| 32 |
}
|
| 33 |
|
| 34 |
-
response = requests.post(
|
| 35 |
-
f"https://api-inference.huggingface.co/models/{model_id}",
|
| 36 |
-
headers=headers,
|
| 37 |
-
json=payload
|
| 38 |
-
)
|
| 39 |
|
| 40 |
result = response.json()
|
| 41 |
-
|
| 42 |
-
return result[0]["
|
| 43 |
-
|
| 44 |
-
return f"❌ Error: {result.get('error',
|
| 45 |
|
| 46 |
# Main logic
|
| 47 |
def generate_and_evaluate(prompt, model_name):
|
| 48 |
model_id = MODEL_OPTIONS[model_name]
|
| 49 |
|
| 50 |
# Step 1: Generate math problem
|
| 51 |
-
generated =
|
| 52 |
|
| 53 |
# Step 2: Evaluate using Qwen 32B
|
| 54 |
eval_prompt = (
|
| 55 |
f"Evaluate the following math problem for originality, clarity, and difficulty. "
|
| 56 |
f"Give a score out of 10 with a short explanation:\n\n{generated}"
|
| 57 |
)
|
| 58 |
-
evaluation =
|
| 59 |
|
| 60 |
return generated.strip(), evaluation.strip()
|
| 61 |
|
|
|
|
| 4 |
|
| 5 |
# Define models
|
| 6 |
MODEL_OPTIONS = {
|
| 7 |
+
"Gemma 3-4B": "google/gemma-3-4b-it:free",
|
| 8 |
+
"Mistral 7B": "mistralai/mistral-7b-instruct:free",
|
| 9 |
+
"Qwen 0.6B": "qwen/qwen3-0.6b-04-28:free",
|
| 10 |
+
"DeepSeek 70B": "deepseek/deepseek-r1-distill-llama-70b:free"
|
| 11 |
}
|
| 12 |
|
| 13 |
EVALUATION_MODEL = "qwen/qwen3-32b"
|
| 14 |
|
| 15 |
+
#Load the open router api
|
| 16 |
+
OPENROUTERAI_API_KEY = os.getenv("OPENROUTERAI")
|
| 17 |
|
| 18 |
# API query function
|
| 19 |
+
def query_openrouter(prompt, model_id):
|
| 20 |
headers = {
|
| 21 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
| 22 |
"Content-Type": "application/json"
|
| 23 |
}
|
| 24 |
|
| 25 |
payload = {
|
| 26 |
+
"model": model_id,
|
| 27 |
+
"messages": [
|
| 28 |
+
{"role": "user", "content": prompt}
|
| 29 |
+
]
|
|
|
|
|
|
|
| 30 |
}
|
| 31 |
|
| 32 |
+
response = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
result = response.json()
|
| 35 |
+
try:
|
| 36 |
+
return result["choices"][0]["message"]["content"]
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return f"❌ Error: {result.get('error', str(e))}"
|
| 39 |
|
| 40 |
# Main logic
|
| 41 |
def generate_and_evaluate(prompt, model_name):
|
| 42 |
model_id = MODEL_OPTIONS[model_name]
|
| 43 |
|
| 44 |
# Step 1: Generate math problem
|
| 45 |
+
generated = query_openrouter(prompt, model_id)
|
| 46 |
|
| 47 |
# Step 2: Evaluate using Qwen 32B
|
| 48 |
eval_prompt = (
|
| 49 |
f"Evaluate the following math problem for originality, clarity, and difficulty. "
|
| 50 |
f"Give a score out of 10 with a short explanation:\n\n{generated}"
|
| 51 |
)
|
| 52 |
+
evaluation = query_openrouter(eval_prompt, EVALUATION_MODEL)
|
| 53 |
|
| 54 |
return generated.strip(), evaluation.strip()
|
| 55 |
|