manopriyonath commited on
Commit
7dc8aa2
·
verified ·
1 Parent(s): 7a4de2d

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +27 -10
main.py CHANGED
@@ -31,31 +31,48 @@ HEADERS = {
31
  "X-Title": "My Free AI App"
32
  }
33
 
34
- MODEL = "google/gemini-2.0-flash-exp" # FREE & fast
35
 
36
  # ---------------------------
37
  # Routes
38
  # ---------------------------
 
39
  @app.get("/")
40
  def home():
41
  return {"message": "Backend is working"}
42
 
43
  @app.get("/chat")
44
  def chat(prompt: str = Query(...)):
 
 
 
 
 
 
 
 
 
 
45
  try:
46
- payload = {
47
- "model": MODEL,
48
- "messages": [
49
- {"role": "user", "content": prompt}
50
- ]
51
- }
52
-
53
- response = requests.post(API_URL, headers=HEADERS, json=payload, timeout=30)
54
- data = response.json()
55
 
 
 
 
 
 
 
 
 
56
  reply = data["choices"][0]["message"]["content"]
57
 
58
  return {"reply": reply}
59
 
60
  except Exception as e:
61
  return {"error": str(e)}
 
 
31
  "X-Title": "My Free AI App"
32
  }
33
 
34
+ MODEL = "meta-llama/llama-3.3-70b-instruct:free" # FREE & fast
35
 
36
  # ---------------------------
37
  # Routes
38
  # ---------------------------
39
+
40
  @app.get("/")
41
  def home():
42
  return {"message": "Backend is working"}
43
 
44
  @app.get("/chat")
45
  def chat(prompt: str = Query(...)):
46
+ if not OPENROUTER_API_KEY:
47
+ return {"error": "OPENROUTER_API_KEY not set in Hugging Face Secrets"}
48
+
49
+ payload = {
50
+ "model": MODEL,
51
+ "messages": [
52
+ {"role": "user", "content": prompt}
53
+ ]
54
+ }
55
+
56
  try:
57
+ response = requests.post(
58
+ API_URL,
59
+ headers=HEADERS,
60
+ json=payload,
61
+ timeout=30
62
+ )
 
 
 
63
 
64
+ if response.status_code != 200:
65
+ return {
66
+ "error": "OpenRouter API error",
67
+ "status_code": response.status_code,
68
+ "details": response.text
69
+ }
70
+
71
+ data = response.json()
72
  reply = data["choices"][0]["message"]["content"]
73
 
74
  return {"reply": reply}
75
 
76
  except Exception as e:
77
  return {"error": str(e)}
78
+