Spaces:
Sleeping
Sleeping
Create hf_llm.py
Browse files
hf_llm.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# hf_llm.py
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# You can change the default model here:
|
| 6 |
+
DEFAULT_MODEL = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 7 |
+
|
| 8 |
+
# Load token from environment variable for security
|
| 9 |
+
HF_API_TOKEN = os.getenv("HF_API_TOKEN", None)
|
| 10 |
+
|
| 11 |
+
# Client setup
|
| 12 |
+
client = InferenceClient(
|
| 13 |
+
model=DEFAULT_MODEL,
|
| 14 |
+
token=HF_API_TOKEN
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
def generate_with_hf(prompt: str, max_new_tokens: int = 256, temperature: float = 0.7) -> str:
|
| 18 |
+
"""
|
| 19 |
+
Generate chat-style responses using Hugging Face text generation models.
|
| 20 |
+
|
| 21 |
+
Args:
|
| 22 |
+
prompt (str): The instruction or user query.
|
| 23 |
+
max_new_tokens (int): Length of output.
|
| 24 |
+
temperature (float): Controls creativity.
|
| 25 |
+
|
| 26 |
+
Returns:
|
| 27 |
+
str: Model response.
|
| 28 |
+
"""
|
| 29 |
+
response = client.text_generation(
|
| 30 |
+
prompt,
|
| 31 |
+
max_new_tokens=max_new_tokens,
|
| 32 |
+
temperature=temperature
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# HF returns raw text
|
| 36 |
+
return response
|