Shauvik889 commited on
Commit
3193c2f
·
verified ·
1 Parent(s): 305f7d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -12
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
- # from dotenv import load_dotenv
7
- # import os
8
 
9
- # load_dotenv()
10
- # hf_token = os.getenv("HUGGINGFACE_TOKEN")
11
 
12
- # client = InferenceClient("microsoft/Phi-3-mini-4k-instruct", token=hf_token)
13
- client = InferenceClient("microsoft/Phi-3-mini-4k-instruct")
 
 
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(server_name="localhost", server_port=7860)
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
- if __name__ == "__main__":
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()