Spaces:
Runtime error
Runtime error
Download app.py.bak from VcRlAgent/TestLLMGen: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/spaces/VcRlAgent/TestLLMGen/resolve/main/app.py.bak
- Command line
-
hf download hf://spaces/VcRlAgent/TestLLMGen/app.py.bak
-
curl -L -o app.py.bak https://huggingface.co/spaces/VcRlAgent/TestLLMGen/resolve/main/app.py.bak
1.1 kB
| import os | |
| import gradio as gr | |
| from openai import OpenAI | |
| from huggingface_hub import InferenceClient | |
| # Initialize HF Router client using OpenAI SDK | |
| ''' | |
| client = OpenAI( | |
| base_url="https://router.huggingface.co/v1", | |
| api_key=os.environ["HF_TOKEN"], # ensure HF_TOKEN is set | |
| ) | |
| ''' | |
| client = InferenceClient(api_key=os.environ["HF_TOKEN"]) | |
| # LLM function | |
| def ask_llm(prompt): | |
| try: | |
| completion = client.chat.completions.create( | |
| model="meta-llama/Llama-3.1-8B-Instruct", | |
| messages=[ | |
| {"role": "user", "content": prompt} | |
| ], | |
| max_tokens=200, | |
| temperature=0.7 | |
| ) | |
| #return completion.choices[0].message["content"] | |
| return completion.choices[0].message.content | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Build Gradio UI | |
| demo = gr.Interface( | |
| fn=ask_llm, | |
| inputs=gr.Textbox(lines=3, label="Ask the AI"), | |
| outputs=gr.Textbox(label="Response"), | |
| title="HF Inference Client LLM Demo", | |
| description="Powered by HuggingFace InferenceClient SDK." | |
| ) | |
| demo.launch() | |