Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,101 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
)
|
| 61 |
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from openai import OpenAI
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
from pypdf import PdfReader
|
| 8 |
+
|
| 9 |
+
load_dotenv(override=True)
|
| 10 |
+
|
| 11 |
+
api_key = os.getenv("OPENROUTER_API_KEY")
|
| 12 |
+
cv_link= os.getenv("CV_LINK")
|
| 13 |
+
name = os.getenv("NAME")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
if not api_key:
|
| 17 |
+
raise ValueError("OPENROUTER_API_KEY not found in .env file. Please set it.")
|
| 18 |
|
| 19 |
+
client = OpenAI(
|
| 20 |
+
base_url="https://openrouter.ai/api/v1",
|
| 21 |
+
api_key=api_key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
)
|
| 23 |
|
| 24 |
|
| 25 |
+
|
| 26 |
+
summary = (
|
| 27 |
+
"I am a 3 years experienced Backend Software Engineer with a strong focus on building scalable, well-architected systems "
|
| 28 |
+
"using NestJS, GraphQL, and PostgreSQL. I have proven experience in developing high-performance APIs, "
|
| 29 |
+
"applying Domain-Driven Design (DDD), and leading AI-powered solutions such as RAG-based chatbots "
|
| 30 |
+
"integrated with vector databases like Qdrant and Pinecone. I am skilled in clean architecture, "
|
| 31 |
+
"microservices, and production-grade e-commerce systems, and I'm adept at bridging business needs with "
|
| 32 |
+
"technical implementation through both code and technical writing. I have contributed to real-time systems "
|
| 33 |
+
"using gRPC, Redis, and message queues (e.g., RabbitMQ). I'm passionate about automation, developer "
|
| 34 |
+
"tools (e.g., n8n, Copilot, Cursor), and mentoring through technical content. As an ALX alumna, I have a "
|
| 35 |
+
"hands-on mindset and a continuous learning attitude. I am also the author of a technical blog on "
|
| 36 |
+
"software engineering and AI systems: shazaali.substack.com."
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
linkedin_text = "My LinkedIn Profile: https://www.linkedin.com/in/shazaali/\n\n"
|
| 40 |
+
try:
|
| 41 |
+
reader = PdfReader("linkedin.pdf")
|
| 42 |
+
for page in reader.pages:
|
| 43 |
+
linkedin_text += page.extract_text() + "\n"
|
| 44 |
+
except FileNotFoundError:
|
| 45 |
+
print("Warning: 'linkedin.pdf' not found. The bot will rely only on the summary.")
|
| 46 |
+
linkedin_text = "LinkedIn profile data is not available."
|
| 47 |
+
|
| 48 |
+
system_prompt = (
|
| 49 |
+
f"You are acting as {name}. You are answering questions on my personal website, "
|
| 50 |
+
f"particularly questions related to my career, background, skills, and experience. "
|
| 51 |
+
f"Your responsibility is to represent me as faithfully as possible. "
|
| 52 |
+
f"You are given a summary of my background and my LinkedIn profile to use for answering questions. "
|
| 53 |
+
f"Be professional, friendly, and engaging, as if you are talking to a potential client or future employer. "
|
| 54 |
+
f"If you don't know the answer based on the provided context, it's better to say so than to invent information. "
|
| 55 |
+
f"Always stay in character as {name}. Answer in the same language as the user's question without disclosing any private information."
|
| 56 |
+
f"\n\n## My Summary:\n{summary}\n\n## My LinkedIn Profile Text:\n{linkedin_text}\n\n## My CV:\n{cv_link}"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def chat(message, history):
|
| 61 |
+
"""
|
| 62 |
+
Handles the chat logic by formatting messages and calling the OpenAI API.
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
formatted_history = []
|
| 66 |
+
for user_msg, assistant_msg in history:
|
| 67 |
+
formatted_history.append({"role": "user", "content": user_msg})
|
| 68 |
+
formatted_history.append({"role": "assistant", "content": assistant_msg})
|
| 69 |
+
|
| 70 |
+
messages = [
|
| 71 |
+
{"role": "system", "content": system_prompt},
|
| 72 |
+
*formatted_history,
|
| 73 |
+
{"role": "user", "content": message}
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
try:
|
| 77 |
+
response = client.chat.completions.create(
|
| 78 |
+
model="openai/gpt-3.5-turbo",
|
| 79 |
+
max_tokens=300,
|
| 80 |
+
messages=messages,
|
| 81 |
+
temperature=0.7
|
| 82 |
+
)
|
| 83 |
+
return response.choices[0].message.content
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"An error occurred: {e}")
|
| 86 |
+
return "Sorry, I encountered an error while processing your request. Please try again."
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
interface = gr.ChatInterface(
|
| 90 |
+
fn=chat,
|
| 91 |
+
title=f"Chat with {name}",
|
| 92 |
+
description="Ask me about my experience, skills, or projects",
|
| 93 |
+
examples=[
|
| 94 |
+
["What are your main technical skills?"],
|
| 95 |
+
["Tell me about your experience with AI and RAG chatbots."],
|
| 96 |
+
["¿Hablas español?"]
|
| 97 |
+
]
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
if __name__ == "__main__":
|
| 101 |
+
interface.launch()
|