Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,158 +1,33 @@
|
|
| 1 |
-
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
client = None
|
| 13 |
-
|
| 14 |
-
def get_inference_client(model_name):
|
| 15 |
-
global client
|
| 16 |
-
if client is None or getattr(client, "model", None) != model_name:
|
| 17 |
-
try:
|
| 18 |
-
client = InferenceClient(model=model_name, token=HF_TOKEN if HF_TOKEN else None)
|
| 19 |
-
print(f"InferenceClient initialized for {model_name}. Token {'provided' if HF_TOKEN else 'not provided'}.")
|
| 20 |
-
except Exception as e:
|
| 21 |
-
print(f"Failed to initialize InferenceClient for {model_name}: {e}")
|
| 22 |
-
return None
|
| 23 |
-
return client
|
| 24 |
-
|
| 25 |
-
def evaluate_understanding(prompt, response):
|
| 26 |
-
if not response or response.strip() == "":
|
| 27 |
-
return "❌ Not Understood (Empty or whitespace response)"
|
| 28 |
-
response_lower = response.lower()
|
| 29 |
-
misunderstanding_keywords = [
|
| 30 |
-
"i'm sorry", "i apologize", "i cannot", "i am unable", "unable to",
|
| 31 |
-
"i don't understand", "could you please rephrase", "i'm not sure i follow",
|
| 32 |
-
"that's not clear", "i do not have enough information", "as an ai language model, i don't",
|
| 33 |
-
"i'm not programmed to", "i lack the ability to"
|
| 34 |
-
]
|
| 35 |
-
for keyword in misunderstanding_keywords:
|
| 36 |
-
if keyword in response_lower:
|
| 37 |
-
return f"⚠️ Potentially Not Understood (Contains: '{keyword}')"
|
| 38 |
-
if len(prompt.split()) > 7 and len(response.split()) < 10:
|
| 39 |
-
return "⚠️ Potentially Not Understood (Response seems too short for the prompt)"
|
| 40 |
-
if prompt.lower() in response_lower and len(response_lower) < len(prompt.lower()) * 1.5:
|
| 41 |
-
if len(prompt.split()) > 5:
|
| 42 |
-
return "⚠️ Potentially Not Understood (Response might be echoing the prompt)"
|
| 43 |
-
return "✔️ Likely Understood"
|
| 44 |
-
|
| 45 |
-
def query_model_and_evaluate(user_prompt, model_name_to_use):
|
| 46 |
-
if not user_prompt or user_prompt.strip() == "":
|
| 47 |
-
return "Please enter a prompt.", "Evaluation N/A", model_name_to_use
|
| 48 |
-
print(f"Querying model: {model_name_to_use}. HF_TOKEN {'is set' if HF_TOKEN else 'is NOT set/empty'}.")
|
| 49 |
-
current_client = get_inference_client(model_name_to_use)
|
| 50 |
-
if current_client is None:
|
| 51 |
-
error_msg = f"Error: Could not initialize the model API client for {model_name_to_use}. Check logs. This might be due to the model requiring authentication (like a token or accepting terms on Hugging Face) which was not available or successful."
|
| 52 |
-
return error_msg, "Evaluation N/A", model_name_to_use
|
| 53 |
try:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
formatted_prompt
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
"max_new_tokens": 300,
|
| 65 |
-
"temperature": 0.6,
|
| 66 |
-
"top_p": 0.9,
|
| 67 |
-
"repetition_penalty": 1.1,
|
| 68 |
-
"do_sample": True,
|
| 69 |
-
"return_full_text": False
|
| 70 |
-
}
|
| 71 |
-
model_response_text = current_client.text_generation(formatted_prompt, **params)
|
| 72 |
-
if not model_response_text:
|
| 73 |
-
model_response_text = ""
|
| 74 |
-
except Exception as e:
|
| 75 |
-
error_message = f"Error calling model API for {model_name_to_use}: {str(e)}. This can happen if the model is gated, requires a Hugging Face token, or if you need to accept its terms of use on the Hugging Face website."
|
| 76 |
-
print(error_message)
|
| 77 |
-
return error_message, "Evaluation N/A", model_name_to_use
|
| 78 |
-
understanding_evaluation = evaluate_understanding(user_prompt, model_response_text)
|
| 79 |
-
return model_response_text, understanding_evaluation, model_name_to_use
|
| 80 |
-
|
| 81 |
-
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="orange")) as demo:
|
| 82 |
-
gr.Markdown(
|
| 83 |
-
f"""
|
| 84 |
-
# 🎯 Model Prompt Understanding Test
|
| 85 |
-
Enter a prompt for the selected language model. The application will send this to the model via Hugging Face's Inference API.
|
| 86 |
-
The model's response will be analyzed to provide a **basic heuristic assessment** of its understanding.
|
| 87 |
-
|
| 88 |
-
**Selected Model:** <span id='current-model-display'>{DEFAULT_MODEL_NAME}</span>
|
| 89 |
-
"""
|
| 90 |
-
)
|
| 91 |
-
current_model_name_state = gr.State(DEFAULT_MODEL_NAME)
|
| 92 |
-
with gr.Row():
|
| 93 |
-
user_input_prompt = gr.Textbox(
|
| 94 |
-
label="✏️ Enter your Prompt:",
|
| 95 |
-
placeholder="e.g., Explain the concept of zero-shot learning in 3 sentences.",
|
| 96 |
-
lines=4,
|
| 97 |
-
scale=3
|
| 98 |
)
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
with gr.Column(scale=1):
|
| 111 |
-
evaluation_output = gr.Textbox(
|
| 112 |
-
label="🧐 Understanding Evaluation:",
|
| 113 |
-
lines=2,
|
| 114 |
-
interactive=False,
|
| 115 |
-
show_copy_button=True
|
| 116 |
-
)
|
| 117 |
-
displayed_model = gr.Textbox(
|
| 118 |
-
label="⚙️ Model Used for this Response:",
|
| 119 |
-
interactive=False,
|
| 120 |
-
lines=1
|
| 121 |
-
)
|
| 122 |
-
submit_button.click(
|
| 123 |
-
fn=query_model_and_evaluate,
|
| 124 |
-
inputs=[user_input_prompt, current_model_name_state],
|
| 125 |
-
outputs=[model_output_response, evaluation_output, displayed_model]
|
| 126 |
-
)
|
| 127 |
-
gr.Markdown(
|
| 128 |
-
"""
|
| 129 |
-
---
|
| 130 |
-
**Disclaimer:**
|
| 131 |
-
* The 'Understanding Evaluation' is a very basic automated heuristic.
|
| 132 |
-
* **Using Models:** This app will attempt to connect to the selected model. Some models (especially gated ones like Llama-2) may require you to have a Hugging Face account, accept their terms of use on the Hugging Face website, and might implicitly require a valid `HF_TOKEN` associated with your account (even if not explicitly set as a secret in this Space). If a model call fails, it could be due to these reasons.
|
| 133 |
-
* Response quality depends heavily on the chosen model and the clarity of your prompt.
|
| 134 |
-
"""
|
| 135 |
-
)
|
| 136 |
-
gr.Examples(
|
| 137 |
-
examples=[
|
| 138 |
-
["Explain the difference between supervised and unsupervised machine learning.", DEFAULT_MODEL_NAME],
|
| 139 |
-
["Write a short poem about a curious robot.", DEFAULT_MODEL_NAME],
|
| 140 |
-
["What are the main challenges in developing AGI?", DEFAULT_MODEL_NAME],
|
| 141 |
-
["Summarize the plot of 'War and Peace' in one paragraph.", DEFAULT_MODEL_NAME],
|
| 142 |
-
["asdfjkl; qwerpoiu", DEFAULT_MODEL_NAME]
|
| 143 |
-
],
|
| 144 |
-
inputs=[user_input_prompt, current_model_name_state],
|
| 145 |
-
outputs=[model_output_response, evaluation_output, displayed_model],
|
| 146 |
-
fn=query_model_and_evaluate,
|
| 147 |
-
cache_examples=False,
|
| 148 |
-
label="💡 Example Prompts (click to try)"
|
| 149 |
-
)
|
| 150 |
-
|
| 151 |
-
if __name__ == "__main__":
|
| 152 |
-
print("Attempting to launch Gradio demo...")
|
| 153 |
-
print(f"Default model: {DEFAULT_MODEL_NAME}")
|
| 154 |
-
if HF_TOKEN:
|
| 155 |
-
print("HF_TOKEN is set.")
|
| 156 |
-
else:
|
| 157 |
-
print("HF_TOKEN is NOT set. Some models (especially gated ones like Llama) might require a token or prior agreement to terms on the Hugging Face website to function correctly. The app will attempt to run, but API calls may fail.")
|
| 158 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.1"
|
| 6 |
+
HF_TOKEN = os.getenv("API_TOKEN_2")
|
| 7 |
|
| 8 |
+
def query_model(prompt):
|
| 9 |
+
if not prompt or not prompt.strip():
|
| 10 |
+
return "Please enter a prompt."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
try:
|
| 12 |
+
client = InferenceClient(model=MODEL_NAME, token=HF_TOKEN)
|
| 13 |
+
formatted_prompt = f"<s>[INST] {prompt.strip()} [/INST]"
|
| 14 |
+
response = client.text_generation(
|
| 15 |
+
formatted_prompt,
|
| 16 |
+
max_new_tokens=300,
|
| 17 |
+
temperature=0.6,
|
| 18 |
+
top_p=0.9,
|
| 19 |
+
repetition_penalty=1.1,
|
| 20 |
+
do_sample=True,
|
| 21 |
+
return_full_text=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
+
return response if response else "(No response from model.)"
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"Error: {str(e)}\n\nThis can happen if the model is gated, requires a Hugging Face token, or you need to accept its terms of use on the Hugging Face website."
|
| 26 |
+
|
| 27 |
+
gr.Interface(
|
| 28 |
+
fn=query_model,
|
| 29 |
+
inputs=gr.Textbox(lines=4, label="Enter your prompt:"),
|
| 30 |
+
outputs=gr.Textbox(lines=10, label="Model Response"),
|
| 31 |
+
title="Simple Mistral-7B-Instruct Demo",
|
| 32 |
+
description="Enter a prompt and get a response from mistralai/Mistral-7B-Instruct-v0.1."
|
| 33 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|