Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,19 @@
|
|
1 |
-
import
|
2 |
import json
|
3 |
-
import
|
4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
|
6 |
def load_model():
|
7 |
-
# Change to the actual TinyLlama model identifier available on Hugging Face.
|
8 |
model_name = "TheBloke/tiny-llama-7b"
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
11 |
return tokenizer, model
|
12 |
|
13 |
-
# Load the model once
|
14 |
tokenizer, model = load_model()
|
15 |
|
16 |
def evaluate_tinyllama(prompt):
|
@@ -18,6 +21,7 @@ def evaluate_tinyllama(prompt):
|
|
18 |
outputs = model.generate(**inputs, max_new_tokens=150)
|
19 |
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
20 |
try:
|
|
|
21 |
result = json.loads(response_text.strip())
|
22 |
except Exception as e:
|
23 |
result = {"stars": 0, "feedback": "Evaluation failed. Unable to parse AI response."}
|
@@ -27,7 +31,7 @@ def evaluate_code(language, question, code):
|
|
27 |
if not code.strip():
|
28 |
return "Error: No code provided. Please enter your solution code."
|
29 |
|
30 |
-
# Build a detailed prompt for the evaluator.
|
31 |
prompt = f"""
|
32 |
You are an expert code evaluator.
|
33 |
Rate the following solution on a scale of 0-5 (0 = completely incorrect, 5 = excellent) and provide a concise feedback message.
|
@@ -38,7 +42,6 @@ Return ONLY valid JSON: {{"stars": number, "feedback": string}}.
|
|
38 |
Do not include any extra text.
|
39 |
"""
|
40 |
result = evaluate_tinyllama(prompt)
|
41 |
-
# Format the output nicely
|
42 |
return f"Stars: {result.get('stars', 0)}\nFeedback: {result.get('feedback', '')}"
|
43 |
|
44 |
iface = gr.Interface(
|
|
|
1 |
+
import os
|
2 |
import json
|
3 |
+
import gradio as gr
|
4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
|
6 |
def load_model():
|
|
|
7 |
model_name = "TheBloke/tiny-llama-7b"
|
8 |
+
token = os.environ.get("HF_TOKEN")
|
9 |
+
if not token:
|
10 |
+
raise ValueError("HF_TOKEN not found in environment variables.")
|
11 |
+
# Load the tokenizer and model using the provided token
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=token)
|
13 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, use_auth_token=token)
|
14 |
return tokenizer, model
|
15 |
|
16 |
+
# Load the model once at startup
|
17 |
tokenizer, model = load_model()
|
18 |
|
19 |
def evaluate_tinyllama(prompt):
|
|
|
21 |
outputs = model.generate(**inputs, max_new_tokens=150)
|
22 |
response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
23 |
try:
|
24 |
+
# Try to parse the model's output as JSON
|
25 |
result = json.loads(response_text.strip())
|
26 |
except Exception as e:
|
27 |
result = {"stars": 0, "feedback": "Evaluation failed. Unable to parse AI response."}
|
|
|
31 |
if not code.strip():
|
32 |
return "Error: No code provided. Please enter your solution code."
|
33 |
|
34 |
+
# Build a detailed prompt for the AI evaluator.
|
35 |
prompt = f"""
|
36 |
You are an expert code evaluator.
|
37 |
Rate the following solution on a scale of 0-5 (0 = completely incorrect, 5 = excellent) and provide a concise feedback message.
|
|
|
42 |
Do not include any extra text.
|
43 |
"""
|
44 |
result = evaluate_tinyllama(prompt)
|
|
|
45 |
return f"Stars: {result.get('stars', 0)}\nFeedback: {result.get('feedback', '')}"
|
46 |
|
47 |
iface = gr.Interface(
|