Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,44 +2,55 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import requests
|
| 4 |
|
|
|
|
| 5 |
MODELS = [
|
| 6 |
"meta-llama/Meta-Llama-3-8B-Instruct",
|
| 7 |
"mistralai/Mistral-7B-Instruct-v0.3",
|
| 8 |
"google/gemma-7b-it",
|
|
|
|
| 9 |
]
|
| 10 |
|
| 11 |
-
# ---------- Chat
|
| 12 |
def chat_with_ai(message, history):
|
| 13 |
if history is None:
|
| 14 |
history = []
|
| 15 |
for model in MODELS:
|
| 16 |
try:
|
| 17 |
client = InferenceClient(model)
|
| 18 |
-
conversation = ""
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
"You are StudyMate AI — a multilingual personal tutor and
|
| 23 |
-
"
|
| 24 |
-
|
| 25 |
-
|
| 26 |
)
|
| 27 |
-
|
| 28 |
reply = ""
|
| 29 |
for chunk in client.text_generation(
|
| 30 |
-
|
| 31 |
-
max_new_tokens=512,
|
| 32 |
-
stream=True,
|
| 33 |
-
temperature=0.7,
|
| 34 |
):
|
| 35 |
reply += chunk.token
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
except Exception:
|
| 39 |
continue
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
return history, ""
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
# ---------- Job Finder ----------
|
| 44 |
def find_jobs(skill):
|
| 45 |
if not skill:
|
|
@@ -86,20 +97,22 @@ def career_guidance(skills, goal, education):
|
|
| 86 |
try:
|
| 87 |
client = InferenceClient(model)
|
| 88 |
prompt = (
|
| 89 |
-
"You are StudyMate AI — a professional career coach
|
| 90 |
-
"
|
| 91 |
-
"
|
| 92 |
-
"
|
| 93 |
-
f"Skills: {skills}\nGoal: {goal}\nEducation: {education}\n\n"
|
| 94 |
-
"Career Recommendations:"
|
| 95 |
)
|
| 96 |
reply = ""
|
| 97 |
-
for chunk in client.text_generation(
|
|
|
|
|
|
|
| 98 |
reply += chunk.token
|
| 99 |
-
|
|
|
|
| 100 |
except Exception:
|
| 101 |
continue
|
| 102 |
-
return "Sorry, I couldn’t
|
|
|
|
| 103 |
|
| 104 |
# ---------- UI Theme ----------
|
| 105 |
css_code = """
|
|
@@ -155,10 +168,9 @@ with gr.Blocks(css=css_code, theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
|
| 155 |
refresh.click(show_job_posts, None, posts_display)
|
| 156 |
|
| 157 |
with gr.Tab("🧭 Career Compass"):
|
| 158 |
-
gr.
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
education = gr.Textbox(label="Education Level (e.g. Intermediate, Graduate, PhD)")
|
| 162 |
generate = gr.Button("Get Career Plan")
|
| 163 |
output = gr.Markdown()
|
| 164 |
generate.click(career_guidance, [skills, goal, education], output)
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import requests
|
| 4 |
|
| 5 |
+
# Main + Fallback Models
|
| 6 |
MODELS = [
|
| 7 |
"meta-llama/Meta-Llama-3-8B-Instruct",
|
| 8 |
"mistralai/Mistral-7B-Instruct-v0.3",
|
| 9 |
"google/gemma-7b-it",
|
| 10 |
+
"HuggingFaceH4/zephyr-7b-beta" # reliable fallback
|
| 11 |
]
|
| 12 |
|
| 13 |
+
# ---------- Chat ----------
|
| 14 |
def chat_with_ai(message, history):
|
| 15 |
if history is None:
|
| 16 |
history = []
|
| 17 |
for model in MODELS:
|
| 18 |
try:
|
| 19 |
client = InferenceClient(model)
|
| 20 |
+
conversation = "".join(
|
| 21 |
+
[f"User: {h[0]}\nStudyMate: {h[1]}\n" for h in history]
|
| 22 |
+
)
|
| 23 |
+
prompt = (
|
| 24 |
+
"You are StudyMate AI — a friendly, multilingual personal tutor and assistant. "
|
| 25 |
+
"You answer *every* question completely in the user’s language (English or Urdu). "
|
| 26 |
+
"If unsure, provide a helpful general explanation or example.\n\n"
|
| 27 |
+
f"{conversation}User: {message}\nStudyMate:"
|
| 28 |
)
|
|
|
|
| 29 |
reply = ""
|
| 30 |
for chunk in client.text_generation(
|
| 31 |
+
prompt, max_new_tokens=512, stream=True, temperature=0.7
|
|
|
|
|
|
|
|
|
|
| 32 |
):
|
| 33 |
reply += chunk.token
|
| 34 |
+
if reply.strip():
|
| 35 |
+
history.append([message, reply])
|
| 36 |
+
return history, ""
|
| 37 |
except Exception:
|
| 38 |
continue
|
| 39 |
+
# Ultimate fallback response
|
| 40 |
+
history.append([message, "I'm having a little trouble fetching a detailed response, but here's a quick idea:\n"
|
| 41 |
+
f"{generate_smart_fallback(message)}"])
|
| 42 |
return history, ""
|
| 43 |
|
| 44 |
+
def generate_smart_fallback(question):
|
| 45 |
+
# Simple built-in emergency logic
|
| 46 |
+
if "job" in question.lower():
|
| 47 |
+
return "Try searching for this in the Job Finder tab — it's updated daily with remote and local roles."
|
| 48 |
+
if "career" in question.lower():
|
| 49 |
+
return "Focus on upskilling and building a small portfolio — it opens many doors faster than degrees alone."
|
| 50 |
+
if "love" in question.lower():
|
| 51 |
+
return "Love and respect go hand in hand — nurture both and life feels lighter."
|
| 52 |
+
return "I’ll keep learning from your feedback — please rephrase or ask again."
|
| 53 |
+
|
| 54 |
# ---------- Job Finder ----------
|
| 55 |
def find_jobs(skill):
|
| 56 |
if not skill:
|
|
|
|
| 97 |
try:
|
| 98 |
client = InferenceClient(model)
|
| 99 |
prompt = (
|
| 100 |
+
"You are StudyMate AI — a professional career coach. "
|
| 101 |
+
"Give career path suggestions, high-demand roles, and study resources. "
|
| 102 |
+
"Write in clear, bullet-point form. If Urdu is detected, reply in Urdu.\n\n"
|
| 103 |
+
f"Skills: {skills}\nGoal: {goal}\nEducation: {education}\n\nCareer Advice:"
|
|
|
|
|
|
|
| 104 |
)
|
| 105 |
reply = ""
|
| 106 |
+
for chunk in client.text_generation(
|
| 107 |
+
prompt, max_new_tokens=600, stream=True, temperature=0.7
|
| 108 |
+
):
|
| 109 |
reply += chunk.token
|
| 110 |
+
if reply.strip():
|
| 111 |
+
return reply
|
| 112 |
except Exception:
|
| 113 |
continue
|
| 114 |
+
return "Sorry, I couldn’t load a detailed career plan, but here’s a quick tip:\n" \
|
| 115 |
+
"Pick one skill you enjoy most and start offering it online — consistent practice beats overthinking."
|
| 116 |
|
| 117 |
# ---------- UI Theme ----------
|
| 118 |
css_code = """
|
|
|
|
| 168 |
refresh.click(show_job_posts, None, posts_display)
|
| 169 |
|
| 170 |
with gr.Tab("🧭 Career Compass"):
|
| 171 |
+
skills = gr.Textbox(label="Your Skills (e.g. Video Editing, Marketing)")
|
| 172 |
+
goal = gr.Textbox(label="Career Goal (e.g. Doctor, Developer, Designer)")
|
| 173 |
+
education = gr.Textbox(label="Education Level (e.g. Intermediate, Graduate)")
|
|
|
|
| 174 |
generate = gr.Button("Get Career Plan")
|
| 175 |
output = gr.Markdown()
|
| 176 |
generate.click(career_guidance, [skills, goal, education], output)
|