YourCoach commited on
Commit
aa62365
·
verified ·
1 Parent(s): 90536e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -71
app.py CHANGED
@@ -4,83 +4,12 @@ import langdetect # Voor taalherkenning
4
  from fuzzywuzzy import process # Voor fuzzy matching
5
  import re # Voor regex matching
6
  import warnings
7
- from fastapi import FastAPI, Depends, HTTPException
8
- from fastapi.security import HTTPBasic, HTTPBasicCredentials
9
- from fastapi.middleware.cors import CORSMiddleware
10
- from starlette.responses import RedirectResponse
11
- import uvicorn
12
 
13
  # Verwijder de warning voor de Levenshtein matcher
14
  warnings.filterwarnings("ignore", message="Using slow pure-python SequenceMatcher.")
15
 
16
- # Initialiseer de InferenceClient
17
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
18
 
19
- # Maak een FastAPI-applicatie aan
20
- app = FastAPI()
21
-
22
- # Basis authenticatie instellen
23
- security = HTTPBasic()
24
-
25
- # Authenticatiefunctie
26
- def authenticate_user(credentials: HTTPBasicCredentials = Depends(security)):
27
- if credentials.username != "admin" or credentials.password != "admin":
28
- raise HTTPException(status_code=401, detail="Invalid credentials")
29
-
30
- # Beschermde route voor authenticatie
31
- @app.get("/login")
32
- async def login(credentials: HTTPBasicCredentials = Depends(security)):
33
- try:
34
- authenticate_user(credentials)
35
- return RedirectResponse("/gradio")
36
- except HTTPException as e:
37
- return {"error": str(e.detail)}
38
-
39
- # Functie voor je Gradio-app
40
- def gradio_interface():
41
- with gr.Blocks() as demo:
42
- gr.Markdown("### Public Section")
43
- input_box = gr.Textbox(label="Enter some text")
44
- output_box = gr.Textbox(label="Response")
45
-
46
- def process_text(text):
47
- try:
48
- detected_language = langdetect.detect(text) # Voor taalherkenning
49
- # Gebruik je InferenceClient hier indien nodig
50
- result = client("Your request here") # Pas dit aan met je verzoek
51
- return f"Detected language: {detected_language}, Result: {result}"
52
- except Exception as e:
53
- return f"Error occurred: {str(e)}"
54
-
55
- input_box.submit(process_text, inputs=input_box, outputs=output_box)
56
- return demo
57
-
58
- # Voeg Gradio toe als FastAPI route
59
- @app.get("/")
60
- async def redirect_to_login():
61
- return RedirectResponse("/login")
62
-
63
- @app.get("/gradio")
64
- async def gradio_route(credentials: HTTPBasicCredentials = Depends(security)):
65
- try:
66
- authenticate_user(credentials)
67
- return gradio_interface()
68
- except HTTPException as e:
69
- return {"error": str(e.detail)}
70
-
71
- # Voeg een middleware toe om CORS te ondersteunen (optioneel)
72
- app.add_middleware(
73
- CORSMiddleware,
74
- allow_origins=["*"],
75
- allow_credentials=True,
76
- allow_methods=["*"],
77
- allow_headers=["*"],
78
- )
79
-
80
- # Start de FastAPI server met Gradio
81
- if __name__ == "__main__":
82
- uvicorn.run(app, host="0.0.0.0", port=7860)
83
-
84
  # Toegestane onderwerpen voor filtering
85
  allowed_topics = [
86
  "love", "relationships", "romance", "dating", "sex", "erotic toys", "sex toys", "sextoys", "intimacy", "divorce", "marriage",
 
4
  from fuzzywuzzy import process # Voor fuzzy matching
5
  import re # Voor regex matching
6
  import warnings
 
 
 
 
 
7
 
8
  # Verwijder de warning voor de Levenshtein matcher
9
  warnings.filterwarnings("ignore", message="Using slow pure-python SequenceMatcher.")
10
 
 
11
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # Toegestane onderwerpen voor filtering
14
  allowed_topics = [
15
  "love", "relationships", "romance", "dating", "sex", "erotic toys", "sex toys", "sextoys", "intimacy", "divorce", "marriage",