Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,44 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def process(prompt):
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
with gr.Blocks() as
|
| 11 |
-
gr.Markdown("
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
|
| 4 |
+
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.1"
|
| 5 |
+
|
| 6 |
+
def format_prompt(user_input):
|
| 7 |
+
return f"[INST] {user_input} [/INST]"
|
| 8 |
+
|
| 9 |
+
def query(payload):
|
| 10 |
+
response = requests.post(API_URL, json=payload)
|
| 11 |
+
return response.json()
|
| 12 |
+
|
| 13 |
+
def evaluate_response(prompt, response):
|
| 14 |
+
prompt_lower = prompt.lower()
|
| 15 |
+
response_lower = response.lower()
|
| 16 |
+
if "capital" in prompt_lower and "france" in prompt_lower:
|
| 17 |
+
return "paris" in response_lower
|
| 18 |
+
return any(indicator in response_lower for indicator in ["answer", "is", "are", "because", ":"])
|
| 19 |
|
| 20 |
def process(prompt):
|
| 21 |
+
try:
|
| 22 |
+
output = query({
|
| 23 |
+
"inputs": format_prompt(prompt),
|
| 24 |
+
"parameters": {
|
| 25 |
+
"max_new_tokens": 100,
|
| 26 |
+
"temperature": 0.3,
|
| 27 |
+
"do_sample": True,
|
| 28 |
+
"return_full_text": False
|
| 29 |
+
}
|
| 30 |
+
})
|
| 31 |
+
response = output[0]['generated_text'].strip()
|
| 32 |
+
evaluation = "✔ Understood" if evaluate_response(prompt, response) else "❌ Didn't understand"
|
| 33 |
+
return f"{response}\n\nEvaluation: {evaluation}"
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"Error: {str(e)}"
|
| 36 |
|
| 37 |
+
with gr.Blocks() as demo:
|
| 38 |
+
gr.Markdown("## Model Understanding Tester")
|
| 39 |
+
prompt_input = gr.Textbox(label="Enter your prompt", lines=3)
|
| 40 |
+
submit_btn = gr.Button("Submit")
|
| 41 |
+
output = gr.Textbox(label="Response", lines=6)
|
| 42 |
+
submit_btn.click(process, inputs=prompt_input, outputs=output)
|
| 43 |
|
| 44 |
+
demo.launch()
|