Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,121 +1,91 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
|
|
|
|
|
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
-
import streamlit.web.bootstrap
|
| 5 |
-
streamlit.web.bootstrap.run('app.py', '', [], {})
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
"""
|
| 13 |
-
<style>
|
| 14 |
-
body {
|
| 15 |
-
background: linear-gradient(to right, #00c6ff, #0072ff);
|
| 16 |
-
color: white;
|
| 17 |
-
}
|
| 18 |
-
.stButton>button {
|
| 19 |
-
background-color: #0044cc;
|
| 20 |
-
color: white;
|
| 21 |
-
border-radius: 10px;
|
| 22 |
-
transition: 0.3s;
|
| 23 |
-
font-size: 18px;
|
| 24 |
-
padding: 10px 20px;
|
| 25 |
-
border: none;
|
| 26 |
-
}
|
| 27 |
-
.stButton>button:hover {
|
| 28 |
-
background-color: #002266;
|
| 29 |
-
transform: scale(1.1);
|
| 30 |
-
box-shadow: 0px 0px 15px rgba(0, 255, 255, 0.6);
|
| 31 |
-
}
|
| 32 |
-
.chat-bubble-user {
|
| 33 |
-
background-color: #0072ff;
|
| 34 |
-
padding: 10px;
|
| 35 |
-
border-radius: 15px;
|
| 36 |
-
margin: 5px;
|
| 37 |
-
text-align: right;
|
| 38 |
-
color: white;
|
| 39 |
-
display: inline-block;
|
| 40 |
-
max-width: 100%;
|
| 41 |
-
}
|
| 42 |
-
.chat-bubble-bot {
|
| 43 |
-
background-color: #0044cc;
|
| 44 |
-
padding: 10px;
|
| 45 |
-
border-radius: 15px;
|
| 46 |
-
margin: 5px;
|
| 47 |
-
text-align: left;
|
| 48 |
-
color: white;
|
| 49 |
-
display: inline-block;
|
| 50 |
-
max-width: 100%;
|
| 51 |
-
}
|
| 52 |
-
</style>
|
| 53 |
-
""",
|
| 54 |
-
unsafe_allow_html=True
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
# **App Title**
|
| 58 |
-
st.title("🤖 VarunGPT")
|
| 59 |
-
st.write("Made in India 🇮🇳")
|
| 60 |
-
st.write("Chat and get programming solutions, news, and much more! 😊")
|
| 61 |
-
|
| 62 |
-
# **API Key Input for Login**
|
| 63 |
-
st.subheader("🔑 Enter your Hugging Face API Key")
|
| 64 |
-
api_key = st.text_input("API Key", type="password")
|
| 65 |
-
|
| 66 |
-
# **Validate API Key**
|
| 67 |
-
if api_key:
|
| 68 |
-
st.success("API Key entered successfully!")
|
| 69 |
-
client = InferenceClient(api_key=api_key)
|
| 70 |
-
else:
|
| 71 |
-
st.warning("Please enter a valid API key to continue.")
|
| 72 |
-
st.stop()
|
| 73 |
-
|
| 74 |
-
# **Sidebar Category**
|
| 75 |
-
theme = st.sidebar.selectbox("Category", ["Fine-Tuned"])
|
| 76 |
-
|
| 77 |
-
# **Initialize Chat History**
|
| 78 |
-
if "chat_history" not in st.session_state:
|
| 79 |
-
st.session_state.chat_history = []
|
| 80 |
-
|
| 81 |
-
# **User Input Box**
|
| 82 |
-
user_input = st.text_input("You: ", "")
|
| 83 |
-
|
| 84 |
-
# **Define function to query Hugging Face Inference API**
|
| 85 |
-
def query_huggingface(prompt):
|
| 86 |
-
messages = [{"role": "user", "content": prompt}]
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
model="mistralai/Mistral-7B-Instruct-v0.3",
|
| 92 |
-
messages=messages,
|
| 93 |
-
max_tokens=500
|
| 94 |
-
)
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
#
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
st.markdown(f"<div class='chat-bubble-bot'><b>🤖 {speaker}:</b> {message}</div>", unsafe_allow_html=True)
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QTextEdit, QPushButton, QLineEdit, QCheckBox, QHBoxLayout
|
| 3 |
+
from PyQt5.QtGui import QFont, QColor, QPalette
|
| 4 |
+
from PyQt5.QtCore import Qt
|
| 5 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
class VarunGPTApp(QWidget):
|
| 8 |
+
def __init__(self):
|
| 9 |
+
super().__init__()
|
| 10 |
+
self.initUI()
|
| 11 |
+
self.chat_history = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
def initUI(self):
|
| 14 |
+
self.setWindowTitle("VarunGPT - AI Chatbot")
|
| 15 |
+
self.setGeometry(200, 200, 700, 550)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
palette = self.palette()
|
| 18 |
+
palette.setColor(QPalette.Window, QColor(20, 20, 20))
|
| 19 |
+
self.setPalette(palette)
|
| 20 |
+
|
| 21 |
+
layout = QVBoxLayout()
|
| 22 |
+
|
| 23 |
+
self.title = QLabel("VarunGPT", self)
|
| 24 |
+
self.title.setFont(QFont("Arial", 22, QFont.Bold))
|
| 25 |
+
self.title.setStyleSheet("color: #ff9800; text-align: center;")
|
| 26 |
+
self.title.setAlignment(Qt.AlignCenter)
|
| 27 |
+
layout.addWidget(self.title)
|
| 28 |
+
|
| 29 |
+
self.api_key_input = QLineEdit(self)
|
| 30 |
+
self.api_key_input.setPlaceholderText("Enter API Key")
|
| 31 |
+
self.api_key_input.setEchoMode(QLineEdit.Password)
|
| 32 |
+
self.api_key_input.setStyleSheet("background-color: #222; color: white; padding: 8px; border-radius: 5px;")
|
| 33 |
+
layout.addWidget(self.api_key_input)
|
| 34 |
+
|
| 35 |
+
self.theme_checkbox = QCheckBox("Enable Advanced Reasoning")
|
| 36 |
+
self.theme_checkbox.setStyleSheet("color: white;")
|
| 37 |
+
layout.addWidget(self.theme_checkbox)
|
| 38 |
+
|
| 39 |
+
self.user_input = QLineEdit(self)
|
| 40 |
+
self.user_input.setPlaceholderText("Ask anything...")
|
| 41 |
+
self.user_input.setStyleSheet("background-color: #222; color: white; padding: 8px; border-radius: 5px;")
|
| 42 |
+
layout.addWidget(self.user_input)
|
| 43 |
+
|
| 44 |
+
self.send_button = QPushButton("Send", self)
|
| 45 |
+
self.send_button.setStyleSheet("background-color: #ff9800; color: white; padding: 10px; border-radius: 5px;")
|
| 46 |
+
self.send_button.clicked.connect(self.handleUserInput)
|
| 47 |
+
layout.addWidget(self.send_button)
|
| 48 |
+
|
| 49 |
+
self.chat_display = QTextEdit(self)
|
| 50 |
+
self.chat_display.setReadOnly(True)
|
| 51 |
+
self.chat_display.setStyleSheet("background-color: #111; color: white; padding: 10px; border-radius: 5px;")
|
| 52 |
+
layout.addWidget(self.chat_display)
|
| 53 |
+
|
| 54 |
+
self.setLayout(layout)
|
| 55 |
+
|
| 56 |
+
def handleUserInput(self):
|
| 57 |
+
api_key = self.api_key_input.text().strip()
|
| 58 |
+
if not api_key:
|
| 59 |
+
self.chat_display.setText("Error: API Key is required.")
|
| 60 |
+
return
|
| 61 |
+
|
| 62 |
+
client = InferenceClient(api_key=api_key)
|
| 63 |
+
|
| 64 |
+
user_text = self.user_input.text()
|
| 65 |
+
if user_text:
|
| 66 |
+
self.chat_history += f"You: {user_text}\n"
|
| 67 |
+
self.chat_display.setText(self.chat_history)
|
| 68 |
+
|
| 69 |
+
ai_response = self.query_huggingface(client, user_text)
|
| 70 |
+
self.chat_history += f"VarunGPT: {ai_response}\n"
|
| 71 |
+
self.chat_display.setText(self.chat_history)
|
| 72 |
+
|
| 73 |
+
self.user_input.clear()
|
| 74 |
+
|
| 75 |
+
def query_huggingface(self, client, prompt):
|
| 76 |
+
messages = [{"role": "user", "content": prompt}]
|
| 77 |
+
try:
|
| 78 |
+
completion = client.chat.completions.create(
|
| 79 |
+
model="mistralai/Mistral-7B-Instruct-v0.3",
|
| 80 |
+
messages=messages,
|
| 81 |
+
max_tokens=500
|
| 82 |
+
)
|
| 83 |
+
return completion["choices"][0]["message"]["content"]
|
| 84 |
+
except Exception as e:
|
| 85 |
+
return f"Error: {str(e)}"
|
| 86 |
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
app = QApplication(sys.argv)
|
| 89 |
+
window = VarunGPTApp()
|
| 90 |
+
window.show()
|
| 91 |
+
sys.exit(app.exec_())
|
|
|