Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
model = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1")
|
| 5 |
+
|
| 6 |
+
def evaluate(prompt, response):
|
| 7 |
+
prompt_lower = prompt.lower()
|
| 8 |
+
response_lower = response.lower()
|
| 9 |
+
keywords = [word for word in prompt_lower.split() if len(word) > 3]
|
| 10 |
+
matches = sum(1 for word in keywords if word in response_lower)
|
| 11 |
+
is_question = any(q in prompt_lower for q in ["who", "what", "when", "where", "why", "how"])
|
| 12 |
+
min_length = max(len(prompt)//2, 10)
|
| 13 |
+
|
| 14 |
+
if is_question:
|
| 15 |
+
understood = any(a in response_lower for a in ["answer", "is", "are", "because"])
|
| 16 |
+
else:
|
| 17 |
+
understood = matches >= len(keywords)//2
|
| 18 |
+
|
| 19 |
+
return understood and len(response) >= min_length
|
| 20 |
+
|
| 21 |
+
def process(prompt):
|
| 22 |
+
response = model(prompt, max_new_tokens=150)[0]['generated_text']
|
| 23 |
+
response = response.replace(prompt, "").strip()
|
| 24 |
+
evaluation = "✔ Understood" if evaluate(prompt, response) else "❌ Didn't understand"
|
| 25 |
+
return f"{response}\n\nEvaluation: {evaluation}"
|
| 26 |
+
|
| 27 |
+
with gr.Blocks() as app:
|
| 28 |
+
gr.Markdown("# Model Understanding Tester")
|
| 29 |
+
inp = gr.Textbox(label="Enter Prompt", lines=3)
|
| 30 |
+
btn = gr.Button("Submit")
|
| 31 |
+
out = gr.Textbox(label="Response", lines=10)
|
| 32 |
+
btn.click(process, inputs=inp, outputs=out)
|
| 33 |
+
|
| 34 |
+
app.launch()
|