Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,17 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
from fastapi import FastAPI, Request
|
| 4 |
-
import uvicorn
|
| 5 |
import threading
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
app = FastAPI()
|
| 16 |
|
|
@@ -42,7 +43,6 @@ def respond(
|
|
| 42 |
temperature=temperature,
|
| 43 |
top_p=top_p,
|
| 44 |
):
|
| 45 |
-
# Only append new tokens if they exist
|
| 46 |
token = msg.choices[0].delta.content
|
| 47 |
if token:
|
| 48 |
response += token
|
|
@@ -60,7 +60,7 @@ def launch_gradio():
|
|
| 60 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 61 |
],
|
| 62 |
)
|
| 63 |
-
demo.launch(
|
| 64 |
|
| 65 |
# Endpoint to access the API via FastAPI
|
| 66 |
@app.post("/generate-response/")
|
|
@@ -79,6 +79,4 @@ async def generate_response(request: Request):
|
|
| 79 |
return {"response": response}
|
| 80 |
|
| 81 |
# Launch Gradio in a separate thread so it doesn't block the API
|
| 82 |
-
|
| 83 |
-
threading.Thread(target=launch_gradio, daemon=True).start()
|
| 84 |
-
uvicorn.run(app, host="localhost", port=8000)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
from fastapi import FastAPI, Request
|
|
|
|
| 4 |
import threading
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# Load environment variables from the .env file
|
| 9 |
+
load_dotenv()
|
| 10 |
|
| 11 |
+
# Get the Hugging Face token from the environment variables
|
| 12 |
+
hf_token = os.getenv("HUGGINGFACE_TOKEN")
|
| 13 |
+
|
| 14 |
+
client = InferenceClient("microsoft/Phi-3-mini-4k-instruct", token=hf_token)
|
| 15 |
|
| 16 |
app = FastAPI()
|
| 17 |
|
|
|
|
| 43 |
temperature=temperature,
|
| 44 |
top_p=top_p,
|
| 45 |
):
|
|
|
|
| 46 |
token = msg.choices[0].delta.content
|
| 47 |
if token:
|
| 48 |
response += token
|
|
|
|
| 60 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
| 61 |
],
|
| 62 |
)
|
| 63 |
+
demo.launch(share=True) # Using share=True to make Gradio accessible on the web
|
| 64 |
|
| 65 |
# Endpoint to access the API via FastAPI
|
| 66 |
@app.post("/generate-response/")
|
|
|
|
| 79 |
return {"response": response}
|
| 80 |
|
| 81 |
# Launch Gradio in a separate thread so it doesn't block the API
|
| 82 |
+
threading.Thread(target=launch_gradio, daemon=True).start()
|
|
|
|
|
|