|
import os |
|
import gradio as gr |
|
import requests |
|
|
|
|
|
HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN") |
|
|
|
|
|
API_URL = "https://api-inference.huggingface.co/models/Orenguteng/Llama-3.1-8B-Lexi-Uncensored-V2" |
|
headers = {"Authorization": f"Bearer {HUGGINGFACE_TOKEN}"} |
|
|
|
def chat(message, history, system_message, max_tokens, temperature, top_p): |
|
|
|
messages = [{"role": "system", "content": system_message}] |
|
|
|
for val in history: |
|
if val[0]: |
|
messages.append({"role": "user", "content": val[0]}) |
|
if val[1]: |
|
messages.append({"role": "assistant", "content": val[1]}) |
|
|
|
messages.append({"role": "user", "content": message}) |
|
|
|
|
|
input_text = "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages]) |
|
|
|
|
|
response = requests.post( |
|
API_URL, |
|
headers=headers, |
|
json={ |
|
"inputs": input_text, |
|
"parameters": { |
|
"max_new_tokens": max_tokens, |
|
"temperature": temperature, |
|
"top_p": top_p, |
|
}, |
|
} |
|
) |
|
|
|
|
|
if response.status_code != 200: |
|
return "Error with API call: " + response.text, history |
|
|
|
|
|
response_text = response.json()[0]['generated_text'].strip() |
|
|
|
|
|
history.append((message, response_text)) |
|
|
|
return response_text, history |
|
|
|
|
|
iface = gr.Interface( |
|
fn=chat, |
|
inputs=[ |
|
gr.Textbox(label="Message"), |
|
gr.State([]), |
|
gr.Textbox(value="You are a friendly Chatbot.", label="System Message"), |
|
gr.Slider(minimum=1, maximum=2048, value=50, step=1, label="Max New Tokens"), |
|
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), |
|
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"), |
|
], |
|
outputs=[ |
|
gr.Textbox(label="Response"), |
|
gr.State([]) |
|
], |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|