sarthak413 commited on
Commit
06771c5
Β·
verified Β·
1 Parent(s): d09f15e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -21
app.py CHANGED
@@ -1,30 +1,97 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
3
  import os
4
 
5
- API_KEY = os.getenv("API_KEY", "your-secret-key-123") # Set in Space Settings
6
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
7
 
8
- def predict(message, api_key):
9
- if api_key != API_KEY:
10
- return {"error": "Invalid API key"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- response = client.chat_completion(
13
- messages=[{"role": "user", "content": message}],
14
- max_tokens=500
15
- )
16
- return {"response": response.choices[0].message.content}
17
-
18
- # Create Gradio interface
19
- with gr.Blocks() as demo:
20
- with gr.Tab("API Endpoint"):
21
- gr.Interface(
22
- fn=predict,
23
- inputs=[gr.Textbox(), gr.Textbox(type="password")],
24
- outputs="json",
25
- api_name="predict" # This creates the /run/predict endpoint
26
  )
27
- with gr.Tab("Chat UI"):
28
- gr.ChatInterface(lambda msg: predict(msg, API_KEY)["response"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
- demo.launch()
 
 
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ from fastapi import FastAPI, HTTPException
4
  import os
5
 
6
+ # Initialize clients
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
+ app = FastAPI()
9
 
10
+ # Configuration - Change this default key for production!
11
+ DEFAULT_SYSTEM_MESSAGE = "You are a chatbot that helps in mood detection and recommends recipes"
12
+ API_TOKEN = "default-secret-key" # πŸ‘ˆ Change this for production deployments
13
+
14
+ def generate_response(
15
+ message: str,
16
+ history: list[tuple[str, str]] = None,
17
+ system_message: str = DEFAULT_SYSTEM_MESSAGE,
18
+ max_tokens: int = 512,
19
+ temperature: float = 0.7,
20
+ top_p: float = 0.95
21
+ ):
22
+ """Core response generation logic"""
23
+ messages = [{"role": "system", "content": system_message}]
24
+
25
+ if history:
26
+ for user_msg, assistant_msg in history:
27
+ if user_msg:
28
+ messages.append({"role": "user", "content": user_msg})
29
+ if assistant_msg:
30
+ messages.append({"role": "assistant", "content": assistant_msg})
31
+
32
+ messages.append({"role": "user", "content": message})
33
+
34
+ try:
35
+ response = ""
36
+ for chunk in client.chat_completion(
37
+ messages,
38
+ max_tokens=max_tokens,
39
+ stream=True,
40
+ temperature=temperature,
41
+ top_p=top_p,
42
+ ):
43
+ response += chunk.choices[0].delta.content or ""
44
+ return response
45
+ except Exception as e:
46
+ raise HTTPException(status_code=500, detail=str(e))
47
+
48
+ # FastAPI Endpoints
49
+ @app.post("/api/chat")
50
+ async def api_chat_endpoint(
51
+ message: str,
52
+ api_token: str,
53
+ system_message: str = DEFAULT_SYSTEM_MESSAGE,
54
+ max_tokens: int = 512,
55
+ temperature: float = 0.7,
56
+ top_p: float = 0.95
57
+ ):
58
+ """External API endpoint"""
59
+ if api_token != API_TOKEN:
60
+ raise HTTPException(status_code=403, detail="Invalid API token")
61
 
62
+ return {
63
+ "response": generate_response(
64
+ message=message,
65
+ system_message=system_message,
66
+ max_tokens=max_tokens,
67
+ temperature=temperature,
68
+ top_p=top_p
 
 
 
 
 
 
 
69
  )
70
+ }
71
+
72
+ # Gradio Interface - Now includes API key validation
73
+ def chat_ui(message: str, history: list[tuple[str, str]], api_key: str):
74
+ if api_key != API_TOKEN:
75
+ return "πŸ”’ Error: Invalid API Key"
76
+ return generate_response(message, history)
77
+
78
+ chat_interface = gr.ChatInterface(
79
+ fn=chat_ui,
80
+ additional_inputs=[
81
+ gr.Textbox(API_TOKEN, label="API Key", type="password"),
82
+ gr.Textbox(DEFAULT_SYSTEM_MESSAGE, label="System message"),
83
+ gr.Slider(1, 2048, 512, step=1, label="Max new tokens"),
84
+ gr.Slider(0.1, 4.0, 0.7, step=0.1, label="Temperature"),
85
+ gr.Slider(0.1, 1.0, 0.95, step=0.05, label="Top-p sampling"),
86
+ ],
87
+ title="🍳 Mood-Based Recipe Assistant",
88
+ description=f"API Key: {API_TOKEN}" # Shows default key for testing
89
+ )
90
+
91
+ # Mount Gradio to FastAPI
92
+ chat_interface.mount_to(app, path="/")
93
 
94
+ if __name__ == "__main__":
95
+ import uvicorn
96
+ print(f"πŸ”‘ Default API Token: {API_TOKEN}") # Visible in console
97
+ uvicorn.run(app, host="0.0.0.0", port=7860)