Spaces:
Running
Running
from huggingface_hub import InferenceClient | |
import gradio as gr | |
def hf_chat(api_key, system_prompt, user_prompt, temperature, max_tokens, top_p): | |
try: | |
# Initialize the Hugging Face Inference Client | |
client = InferenceClient(api_key=api_key) | |
# Prepare the messages | |
messages = [ | |
{"role": "system", "content": system_prompt}, | |
{"role": "user", "content": user_prompt} | |
] | |
# Stream the response from the Hugging Face model | |
stream = client.chat.completions.create( | |
model="Qwen/Qwen2.5-72B-Instruct", | |
messages=messages, | |
temperature=temperature, | |
max_tokens=max_tokens, | |
top_p=top_p, | |
stream=True | |
) | |
# Concatenate the streamed content | |
output = "" | |
for chunk in stream: | |
output += chunk.choices[0].delta.content | |
return output | |
except Exception as e: | |
return f"Error: {str(e)}" | |
# Define the Gradio interface | |
with gr.Blocks() as demo: | |
gr.Markdown("""# history prof it is my first appli. | |
i am bad in history but to help me for my homework i makeing | |
a ai.""") | |
api_key_input = gr.Textbox(label="Hugging Face API Key", placeholder="Enter your Hugging Face API key", type="password") | |
system_prompt_input = gr.Textbox(label="ethan's history prof", value="You are a history professor 5e in FRENCH.", placeholder="you are a history professor 5e in FRENCH.") | |
user_prompt_input = gr.Textbox(label="user chat", placeholder="metez votre question ici.") | |
temperature_slider = gr.Slider(label="Temperature", minimum=0.0, maximum=1.0, value=0.7, step=0.1) | |
max_tokens_slider = gr.Slider(label="Max Tokens", minimum=10, maximum=2048, value=100, step=10) | |
top_p_slider = gr.Slider(label="Top P", minimum=0.1, maximum=1.0, value=0.7, step=0.1) | |
output = gr.Textbox(label="history prof π¨βπ«") | |
generate_button = gr.Button("π") | |
generate_button.click( | |
hf_chat, | |
inputs=[ | |
api_key_input, | |
system_prompt_input, | |
user_prompt_input, | |
temperature_slider, | |
max_tokens_slider, | |
top_p_slider | |
], | |
outputs=[output] | |
) | |
if __name__ == "__main__": | |
demo.launch() | |
#Merci papa de m'avoir offert cet ordi β€οΈβ€οΈβ€οΈ |