Spaces:
Runtime error
Runtime error
Download app.py from EinfachAlexGPT/PromptEngineer_LeCun: direct link, hf CLI and curl.
- Browser
- Download file 3.06 kB
-
https://huggingface.co/spaces/EinfachAlexGPT/PromptEngineer_LeCun/resolve/main/app.py
- Command line
-
hf download hf://spaces/EinfachAlexGPT/PromptEngineer_LeCun/app.py
-
curl -L -o app.py https://huggingface.co/spaces/EinfachAlexGPT/PromptEngineer_LeCun/resolve/main/app.py
3.06 kB
| from huggingface_hub import InferenceClient | |
| import gradio as gr | |
| import os | |
| import cachetools | |
| # Caching für Inferenz-Resultate (max 100 Einträge) | |
| cache = cachetools.LRUCache(maxsize=100) | |
| # Klient für die Inferenz (hier könnte Mistral 7B oder GGUF verwendet werden) | |
| client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1") | |
| # Geheime Eingabeaufforderung aus Umgebungsvariablen | |
| secret_prompt = os.getenv("SECRET_PROMPT") | |
| # Funktion zum Formatieren der Nachrichtenhistorie | |
| def format_prompt(new_message, history): | |
| prompt = secret_prompt | |
| for user_msg, bot_msg in history: | |
| prompt += f"[INST] {user_msg} [/INST] {bot_msg}</s>" | |
| prompt += f"[INST] {new_message} [/INST]" | |
| return prompt | |
| # Fehlerbehandlung und API-Caching in die Generierung einbauen | |
| def generate(prompt, history, temperature=0.25, max_new_tokens=1024, top_p=0.95, repetition_penalty=1.0): | |
| try: | |
| # Cache-Prüfung | |
| if prompt in cache: | |
| return cache[prompt] | |
| # Konfiguration der Parameter | |
| temperature = max(0.01, float(temperature)) # Sicherheitscheck für min. Wert | |
| top_p = float(top_p) | |
| generate_kwargs = { | |
| "temperature": temperature, | |
| "max_new_tokens": int(max_new_tokens), | |
| "top_p": top_p, | |
| "repetition_penalty": float(repetition_penalty), | |
| "do_sample": True, | |
| "seed": 727, | |
| } | |
| formatted_prompt = format_prompt(prompt, history) | |
| # Inferenz starten und im Stream verarbeiten | |
| stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False) | |
| output = "" | |
| for response in stream: | |
| output += response.token.text | |
| yield output | |
| # Cache speichern | |
| cache[prompt] = output | |
| return output | |
| except Exception as e: | |
| yield f"Fehler bei der Verarbeitung: {str(e)}" | |
| # Gradio-UI mit interaktiven Parametern | |
| with gr.Blocks(theme="gradio/ghostly") as demo: | |
| with gr.Row(): | |
| chatbot = gr.Chatbot(label="Samir Chatbot", bubble_full_width=True, show_label=False, show_copy_button=False) | |
| with gr.Column(): | |
| temp_slider = gr.Slider(minimum=0.01, maximum=1.0, value=0.25, label="Temperature") | |
| token_slider = gr.Slider(minimum=16, maximum=2048, value=1024, step=1, label="Max Tokens") | |
| top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, label="Top-p") | |
| # Eingabefeld für Benutzereingaben | |
| user_input = gr.Textbox(label="Deine Nachricht", placeholder="Gib eine Nachricht ein...") | |
| submit_button = gr.Button("Generieren") | |
| # Benutzer-Interaktion | |
| def user_interaction(user_message, history, temperature, max_tokens, top_p): | |
| return generate(user_message, history, temperature, max_tokens, top_p) | |
| submit_button.click(user_interaction, [user_input, chatbot, temp_slider, token_slider, top_p_slider], chatbot) | |
| # Asynchrones Starten der UI | |
| demo.queue().launch(show_api=False) | |