Spaces:
Sleeping
Sleeping
File size: 2,367 Bytes
2d8d112 |
1 2 3 4 5 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
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 β€οΈβ€οΈβ€οΈ |