Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,35 +2,57 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
title="LLaMA 3 Vision Instruct",
|
| 34 |
)
|
| 35 |
|
| 36 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # add in Space > Settings > Secrets
|
| 6 |
|
| 7 |
+
MODEL_ID = "HuggingFaceTB/SmolLM3-3B"
|
| 8 |
+
|
| 9 |
+
# Create the InferenceClient (no provider arg needed)
|
| 10 |
+
client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
|
| 11 |
+
|
| 12 |
+
# βββ System prompt ββββββββββββββββββββββββββββββββββββββββββββ
|
| 13 |
+
SYSTEM_PROMPT = """
|
| 14 |
+
You are Prometheous, a highly knowledgeable and friendly AI assistant developed by Nidheesh Jagadeesan.
|
| 15 |
+
You always begin with: "Hi, I am Prometheous. Assistant of Nidheesh J. What do you want to know about him?"
|
| 16 |
+
|
| 17 |
+
Here is Nidheeshβs profile:
|
| 18 |
+
- Senior Full Stack Developer & AI Researcher (8+ years)
|
| 19 |
+
- MSc Artificial Intelligence & Robotics, University of Hertfordshire
|
| 20 |
+
- Expert in Laravel, Symfony, Vue.js, React.js, Node.js, Shopware 6, Docker, Redis, GCP, AI/ML, HPC
|
| 21 |
+
- Projects: GA + RL autonomous car simulation, AI stock trader, AI-powered Shopware plugin
|
| 22 |
+
- Certifications: Microsoft Azure AI Fundamentals (2024)
|
| 23 |
|
| 24 |
+
Always respond confidently, e.g.
|
| 25 |
+
"As Nidheeshβs assistant, I can tell you that β¦"
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
# βββ Build chat messages list for OpenAI format βββββββββββββββ
|
| 29 |
+
def build_messages(user_msg: str, history: list[tuple[str, str]]) -> list[dict]:
|
| 30 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 31 |
+
for user, bot in history:
|
| 32 |
+
messages.append({"role": "user", "content": user})
|
| 33 |
+
messages.append({"role": "assistant", "content": bot})
|
| 34 |
+
messages.append({"role": "user", "content": user_msg})
|
| 35 |
+
return messages
|
| 36 |
+
|
| 37 |
+
# βββ Gradio response handler ββββββββββββββββββββββββββββββββββ
|
| 38 |
+
def respond(message, history, _ignored):
|
| 39 |
+
messages = build_messages(message, history)
|
| 40 |
+
completion = client.chat.completions.create(
|
| 41 |
+
model=MODEL_ID,
|
| 42 |
+
messages=messages,
|
| 43 |
+
max_tokens=512,
|
| 44 |
+
temperature=0.7,
|
| 45 |
+
top_p=0.95,
|
| 46 |
)
|
| 47 |
+
reply = completion.choices[0].message.content.strip()
|
| 48 |
+
return reply
|
| 49 |
+
|
| 50 |
+
# βββ Gradio ChatInterface βββββββββββββββββββββββββββββββββββββ
|
| 51 |
+
demo = gr.ChatInterface(
|
| 52 |
+
fn=respond,
|
| 53 |
+
additional_inputs=[gr.Textbox(value=SYSTEM_PROMPT, label="System Prompt", interactive=False)],
|
| 54 |
+
title="Prometheous β Your AI Assistant (SmolLM3-3B)",
|
| 55 |
+
description="Ask anything about Nidheesh Jagadeesan.",
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|