charantejapolavarapu commited on
Commit
128c2ce
·
verified ·
1 Parent(s): 6c175be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -26
app.py CHANGED
@@ -4,43 +4,41 @@ import os
4
  import re
5
  import time
6
 
7
- # Mistral is reliable and follows interview instructions well
8
- API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
 
 
9
  HF_TOKEN = os.getenv("HF_TOKEN")
10
 
11
- headers = {"Authorization": f"Bearer {HF_TOKEN}"}
12
 
13
  def query_llm(prompt):
14
- """Helper to call API with automatic retry and wait-for-model logic."""
15
  payload = {
16
  "inputs": f"<s>[INST] {prompt} [/INST]",
17
  "parameters": {"max_new_tokens": 250, "temperature": 0.7},
18
- "options": {"wait_for_model": True} # Tells HF to wait for the model to load
19
  }
20
 
21
- for attempt in range(3): # Try up to 3 times
22
- try:
23
- response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
24
- result = response.json()
25
-
26
- # If model is still loading despite wait_for_model
27
- if isinstance(result, dict) and "error" in result:
28
- if "loading" in result["error"].lower():
29
- time.sleep(10) # Wait 10 seconds before retrying
30
- continue
31
- return f"AI Error: {result['error']}"
32
-
33
- if isinstance(result, list) and len(result) > 0:
34
- return result[0]["generated_text"].split("[/INST]")[-1].strip()
35
 
36
- except Exception as e:
37
- if attempt == 2: return f"Connection Error: {str(e)}"
38
- time.sleep(5)
39
 
40
- return "The model is taking too long to wake up. Please wait 1 minute and refresh."
 
 
 
41
 
42
  # ----------------------------
43
- # AI Functions
44
  # ----------------------------
45
 
46
  def generate_question(role, difficulty, resume_text):
@@ -67,7 +65,7 @@ def evaluate_answer(question, answer):
67
  # ----------------------------
68
 
69
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
70
- gr.Markdown("# 🤖 AI Interview Simulator")
71
 
72
  with gr.Row():
73
  role = gr.Textbox(label="Job Role", placeholder="e.g. Data Analyst")
@@ -88,4 +86,5 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
88
  gen_btn.click(generate_question, [role, diff, resume], q_display)
89
  eval_btn.click(evaluate_answer, [q_display, ans_input], [fb_display, score_display])
90
 
91
- demo.launch()
 
 
4
  import re
5
  import time
6
 
7
+ # NEW ROUTER URL
8
+ # Format: https://router.huggingface.co/hf-inference/models/{model_id}
9
+ MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.3"
10
+ API_URL = f"https://router.huggingface.co/hf-inference/models/{MODEL_ID}"
11
  HF_TOKEN = os.getenv("HF_TOKEN")
12
 
13
+ headers = {"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"}
14
 
15
  def query_llm(prompt):
 
16
  payload = {
17
  "inputs": f"<s>[INST] {prompt} [/INST]",
18
  "parameters": {"max_new_tokens": 250, "temperature": 0.7},
19
+ "options": {"wait_for_model": True}
20
  }
21
 
22
+ try:
23
+ # The new router endpoint
24
+ response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
25
+
26
+ # Check for error messages from the router
27
+ if response.status_code != 200:
28
+ error_detail = response.json().get("error", "Unknown Router Error")
29
+ return f"AI Error ({response.status_code}): {error_detail}"
 
 
 
 
 
 
30
 
31
+ result = response.json()
32
+ if isinstance(result, list) and len(result) > 0:
33
+ return result[0]["generated_text"].split("[/INST]")[-1].strip()
34
 
35
+ return "Unexpected response format from AI."
36
+
37
+ except Exception as e:
38
+ return f"Connection Error: {str(e)}"
39
 
40
  # ----------------------------
41
+ # AI Functions (Logic remains the same)
42
  # ----------------------------
43
 
44
  def generate_question(role, difficulty, resume_text):
 
65
  # ----------------------------
66
 
67
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
68
+ gr.Markdown("# 🤖 AI Interview Simulator (Updated Router)")
69
 
70
  with gr.Row():
71
  role = gr.Textbox(label="Job Role", placeholder="e.g. Data Analyst")
 
86
  gen_btn.click(generate_question, [role, diff, resume], q_display)
87
  eval_btn.click(evaluate_answer, [q_display, ans_input], [fb_display, score_display])
88
 
89
+ if __name__ == "__main__":
90
+ demo.launch()