VcRlAgent commited on
Commit
07e4e32
·
1 Parent(s): 956bbf8

Starter LLM Inference Call

Browse files
Files changed (2) hide show
  1. app.py +35 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from openai import OpenAI
4
+
5
+ # Initialize HF Router client using OpenAI SDK
6
+ client = OpenAI(
7
+ base_url="https://router.huggingface.co/v1",
8
+ api_key=os.environ["HF_TOKEN"], # ensure HF_TOKEN is set
9
+ )
10
+
11
+ # LLM function
12
+ def ask_llm(prompt):
13
+ try:
14
+ completion = client.chat.completions.create(
15
+ model="meta-llama/Llama-3.1-8B-Instruct",
16
+ messages=[
17
+ {"role": "user", "content": prompt}
18
+ ],
19
+ max_tokens=200
20
+ )
21
+ return completion.choices[0].message["content"]
22
+ except Exception as e:
23
+ return f"Error: {str(e)}"
24
+
25
+
26
+ # Build Gradio UI
27
+ demo = gr.Interface(
28
+ fn=ask_llm,
29
+ inputs=gr.Textbox(lines=3, label="Ask the AI"),
30
+ outputs=gr.Textbox(label="Response"),
31
+ title="HF Router LLM Demo",
32
+ description="Powered by HuggingFace Router + OpenAI SDK client."
33
+ )
34
+
35
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ openai>=1.51.0