Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import InferenceClient
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# 1) Put your token here or set HF_TOKEN in env
|
| 5 |
+
API_KEY = os.getenv("HF_TOKEN", "hf_your_token_here")
|
| 6 |
+
|
| 7 |
+
client = InferenceClient(api_key=API_KEY)
|
| 8 |
+
|
| 9 |
+
SYSTEM_PROMPT = "You are a helpful assistant in a medical AI course. Answer clearly and safely."
|
| 10 |
+
|
| 11 |
+
PARAMS = {
|
| 12 |
+
"max_tokens": 300,
|
| 13 |
+
"temperature": 0.7,
|
| 14 |
+
"top_p": 0.9,
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
def simple_chat(user_message: str, model_name: str):
|
| 18 |
+
if not user_message.strip():
|
| 19 |
+
return "Please ask a question!"
|
| 20 |
+
|
| 21 |
+
messages = [
|
| 22 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 23 |
+
{"role": "user", "content": user_message},
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
completion = client.chat.completions.create(
|
| 28 |
+
model=model_name,
|
| 29 |
+
messages=messages,
|
| 30 |
+
max_tokens=PARAMS["max_tokens"],
|
| 31 |
+
temperature=PARAMS["temperature"],
|
| 32 |
+
top_p=PARAMS["top_p"],
|
| 33 |
+
)
|
| 34 |
+
return completion.choices[0].message.content
|
| 35 |
+
except Exception as e:
|
| 36 |
+
# Print real error so you can debug
|
| 37 |
+
return f"Raw error from Hugging Face:\n{repr(e)}"
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
# Start with ONE model you know is served by Inference Providers:
|
| 42 |
+
test_model = "HuggingFaceTB/SmolLM3-3B"
|
| 43 |
+
print("Testing model:", test_model)
|
| 44 |
+
print(simple_chat("Say hello in one sentence.", test_model))
|