freddyaboulton HF Staff commited on
Commit
4434516
·
verified ·
1 Parent(s): afd68a5

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +6 -6
  2. requirements.txt +1 -0
  3. run.ipynb +1 -0
  4. run.py +28 -0
README.md CHANGED
@@ -1,12 +1,12 @@
 
1
  ---
2
- title: Llm Hyperbolic
3
- emoji: 📚
4
  colorFrom: indigo
5
  colorTo: indigo
6
  sdk: gradio
7
- sdk_version: 5.7.0
8
- app_file: app.py
9
  pinned: false
 
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+
2
  ---
3
+ title: llm_hyperbolic
4
+ emoji: 🔥
5
  colorFrom: indigo
6
  colorTo: indigo
7
  sdk: gradio
8
+ sdk_version: 5.7.1
9
+ app_file: run.py
10
  pinned: false
11
+ hf_oauth: true
12
  ---
 
 
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ openai>=1.0.0
run.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: llm_hyperbolic"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio openai>=1.0.0 "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["# This is a simple general-purpose chatbot built on top of Hyperbolic API. \n", "# Before running this, make sure you have exported your Hyperbolic API key as an environment variable:\n", "# export HYPERBOLIC_API_KEY=\"your-hyperbolic-api-key\"\n", "\n", "import os\n", "import gradio as gr\n", "from openai import OpenAI\n", "\n", "api_key = os.getenv(\"HYPERBOLIC_API_KEY\")\n", "\n", "client = OpenAI(\n", " base_url=\"https://api.hyperbolic.xyz/v1/\",\n", " api_key=api_key,\n", ")\n", "\n", "def predict(message, history):\n", " history.append({\"role\": \"user\", \"content\": message})\n", " stream = client.chat.completions.create(messages=history, model=\"gpt-4o-mini\", stream=True)\n", " chunks = []\n", " for chunk in stream:\n", " chunks.append(chunk.choices[0].delta.content or \"\")\n", " yield \"\".join(chunks)\n", "\n", "demo = gr.ChatInterface(predict, type=\"messages\")\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n", "\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This is a simple general-purpose chatbot built on top of Hyperbolic API.
2
+ # Before running this, make sure you have exported your Hyperbolic API key as an environment variable:
3
+ # export HYPERBOLIC_API_KEY="your-hyperbolic-api-key"
4
+
5
+ import os
6
+ import gradio as gr
7
+ from openai import OpenAI
8
+
9
+ api_key = os.getenv("HYPERBOLIC_API_KEY")
10
+
11
+ client = OpenAI(
12
+ base_url="https://api.hyperbolic.xyz/v1/",
13
+ api_key=api_key,
14
+ )
15
+
16
+ def predict(message, history):
17
+ history.append({"role": "user", "content": message})
18
+ stream = client.chat.completions.create(messages=history, model="gpt-4o-mini", stream=True)
19
+ chunks = []
20
+ for chunk in stream:
21
+ chunks.append(chunk.choices[0].delta.content or "")
22
+ yield "".join(chunks)
23
+
24
+ demo = gr.ChatInterface(predict, type="messages")
25
+
26
+ if __name__ == "__main__":
27
+ demo.launch()
28
+