Spaces:
No application file
No application file
DANIEL PLSSS ADD THIS
#1
by dannjoIIai - opened
- LLaMa backup +41 -0
LLaMa backup
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
|
| 5 |
+
# Initialize the Hugging Face Inference Client
|
| 6 |
+
# It automatically picks up the system HF_TOKEN if needed, or runs anonymously for public models
|
| 7 |
+
client = InferenceClient()
|
| 8 |
+
|
| 9 |
+
def predict(message, history):
|
| 10 |
+
# Format the entire chat history for an LLM instruction model
|
| 11 |
+
messages = []
|
| 12 |
+
for user_msg, assistant_msg in history:
|
| 13 |
+
messages.append({"role": "user", "content": user_msg})
|
| 14 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 15 |
+
|
| 16 |
+
# Append the newest message
|
| 17 |
+
messages.append({"role": "user", "content": message})
|
| 18 |
+
|
| 19 |
+
# Request a streaming response from a powerful, free-tier chat model
|
| 20 |
+
response = ""
|
| 21 |
+
for token in client.chat_completion(
|
| 22 |
+
model="meta-llama/Llama-3.2-3B-Instruct",
|
| 23 |
+
messages=messages,
|
| 24 |
+
max_tokens=500,
|
| 25 |
+
stream=True
|
| 26 |
+
):
|
| 27 |
+
token_text = token.choices[0].delta.content
|
| 28 |
+
if token_text:
|
| 29 |
+
response += token_text
|
| 30 |
+
yield response
|
| 31 |
+
|
| 32 |
+
# Build a beautiful, responsive Gradio ChatInterface
|
| 33 |
+
demo = gr.ChatInterface(
|
| 34 |
+
fn=predict,
|
| 35 |
+
title="My Custom Chatbot",
|
| 36 |
+
description="Ask me anything! Powered by Llama 3.2 via Hugging Face Inference.",
|
| 37 |
+
examples=["Explain quantum computing simply.", "Write a poem about a coding error."]
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
demo.launch()
|