Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import torch
|
|
| 6 |
# Load Fine-Tuned GPT-2 Model
|
| 7 |
# =========================
|
| 8 |
def load_model():
|
| 9 |
-
model_path = "zaid002/finetunedmodel"
|
| 10 |
tokenizer = GPT2Tokenizer.from_pretrained(model_path)
|
| 11 |
model = GPT2LMHeadModel.from_pretrained(model_path)
|
| 12 |
|
|
@@ -22,48 +22,52 @@ def load_model():
|
|
| 22 |
tokenizer, chatbot = load_model()
|
| 23 |
|
| 24 |
# =========================
|
| 25 |
-
# ChatGPT
|
| 26 |
# =========================
|
| 27 |
SYSTEM_PROMPT = """
|
| 28 |
-
You are
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
- Programming (Python, Java, SQL, ML, AI, Data Science)
|
| 32 |
- Interview preparation
|
|
|
|
| 33 |
- General knowledge
|
| 34 |
-
- Education and career guidance
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
"""
|
| 39 |
|
| 40 |
# =========================
|
| 41 |
-
# Chat Function
|
| 42 |
# =========================
|
| 43 |
-
def
|
| 44 |
-
|
| 45 |
|
| 46 |
-
# Add
|
| 47 |
for user, bot in history:
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
-
conversation += f"User: {user_input}\nAssistant:"
|
| 52 |
|
| 53 |
output = chatbot(
|
| 54 |
-
|
| 55 |
-
max_length=
|
| 56 |
do_sample=True,
|
| 57 |
temperature=0.7,
|
| 58 |
top_p=0.9,
|
| 59 |
top_k=50,
|
|
|
|
| 60 |
pad_token_id=tokenizer.eos_token_id
|
| 61 |
)
|
| 62 |
|
| 63 |
response = output[0]["generated_text"].split("Assistant:")[-1].strip()
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
response = response[:
|
| 67 |
|
| 68 |
history.append((user_input, response))
|
| 69 |
return history, history
|
|
@@ -72,29 +76,28 @@ def chatgpt_style_chat(user_input, history):
|
|
| 72 |
# Gradio UI
|
| 73 |
# =========================
|
| 74 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 75 |
-
gr.HTML(
|
| 76 |
-
|
| 77 |
-
<
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
"""
|
| 82 |
-
)
|
| 83 |
|
| 84 |
chatbot_ui = gr.Chatbot(height=450)
|
|
|
|
| 85 |
msg = gr.Textbox(
|
| 86 |
-
placeholder="💬 Ask
|
| 87 |
label="Your Message"
|
| 88 |
)
|
| 89 |
|
| 90 |
state = gr.State([])
|
| 91 |
clear = gr.Button("🗑️ Clear Chat")
|
| 92 |
|
| 93 |
-
msg.submit(
|
| 94 |
clear.click(lambda: ([], []), None, [chatbot_ui, state], queue=False)
|
| 95 |
|
| 96 |
# =========================
|
| 97 |
-
# Launch
|
| 98 |
# =========================
|
| 99 |
if __name__ == "__main__":
|
| 100 |
demo.launch()
|
|
|
|
| 6 |
# Load Fine-Tuned GPT-2 Model
|
| 7 |
# =========================
|
| 8 |
def load_model():
|
| 9 |
+
model_path = "zaid002/finetunedmodel" # Hugging Face repo
|
| 10 |
tokenizer = GPT2Tokenizer.from_pretrained(model_path)
|
| 11 |
model = GPT2LMHeadModel.from_pretrained(model_path)
|
| 12 |
|
|
|
|
| 22 |
tokenizer, chatbot = load_model()
|
| 23 |
|
| 24 |
# =========================
|
| 25 |
+
# ChatGPT System Prompt
|
| 26 |
# =========================
|
| 27 |
SYSTEM_PROMPT = """
|
| 28 |
+
You are a powerful AI assistant like ChatGPT.
|
| 29 |
+
|
| 30 |
+
You can answer:
|
| 31 |
+
- GUVI course-related questions
|
| 32 |
- Programming (Python, Java, SQL, ML, AI, Data Science)
|
| 33 |
- Interview preparation
|
| 34 |
+
- Career guidance
|
| 35 |
- General knowledge
|
|
|
|
| 36 |
|
| 37 |
+
Rules:
|
| 38 |
+
- Respond in the SAME language as the user
|
| 39 |
+
- Be clear, helpful, and human-like
|
| 40 |
+
- Give structured answers when needed
|
| 41 |
+
- Avoid repetition
|
| 42 |
"""
|
| 43 |
|
| 44 |
# =========================
|
| 45 |
+
# Chat Function (ChatGPT Style)
|
| 46 |
# =========================
|
| 47 |
+
def chatgpt_multilingual(user_input, history):
|
| 48 |
+
prompt = SYSTEM_PROMPT + "\n\n"
|
| 49 |
|
| 50 |
+
# Add conversation memory
|
| 51 |
for user, bot in history:
|
| 52 |
+
prompt += f"User: {user}\nAssistant: {bot}\n"
|
| 53 |
|
| 54 |
+
prompt += f"User: {user_input}\nAssistant:"
|
|
|
|
| 55 |
|
| 56 |
output = chatbot(
|
| 57 |
+
prompt,
|
| 58 |
+
max_length=400,
|
| 59 |
do_sample=True,
|
| 60 |
temperature=0.7,
|
| 61 |
top_p=0.9,
|
| 62 |
top_k=50,
|
| 63 |
+
repetition_penalty=1.2,
|
| 64 |
pad_token_id=tokenizer.eos_token_id
|
| 65 |
)
|
| 66 |
|
| 67 |
response = output[0]["generated_text"].split("Assistant:")[-1].strip()
|
| 68 |
|
| 69 |
+
# Safety trim
|
| 70 |
+
response = response[:600]
|
| 71 |
|
| 72 |
history.append((user_input, response))
|
| 73 |
return history, history
|
|
|
|
| 76 |
# Gradio UI
|
| 77 |
# =========================
|
| 78 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 79 |
+
gr.HTML("""
|
| 80 |
+
<div style="text-align:center;">
|
| 81 |
+
<h1>🤖 GUVI Multilingual ChatGPT Bot</h1>
|
| 82 |
+
<p>Ask anything about GUVI, programming, careers, or general topics — in ANY language.</p>
|
| 83 |
+
</div>
|
| 84 |
+
""")
|
|
|
|
|
|
|
| 85 |
|
| 86 |
chatbot_ui = gr.Chatbot(height=450)
|
| 87 |
+
|
| 88 |
msg = gr.Textbox(
|
| 89 |
+
placeholder="💬 Ask in English / हिंदी / தமிழ் / తెలుగు / Français / Español / etc.",
|
| 90 |
label="Your Message"
|
| 91 |
)
|
| 92 |
|
| 93 |
state = gr.State([])
|
| 94 |
clear = gr.Button("🗑️ Clear Chat")
|
| 95 |
|
| 96 |
+
msg.submit(chatgpt_multilingual, [msg, state], [chatbot_ui, state])
|
| 97 |
clear.click(lambda: ([], []), None, [chatbot_ui, state], queue=False)
|
| 98 |
|
| 99 |
# =========================
|
| 100 |
+
# Launch App
|
| 101 |
# =========================
|
| 102 |
if __name__ == "__main__":
|
| 103 |
demo.launch()
|