Update app.py
Browse files
app.py
CHANGED
|
@@ -1,78 +1,76 @@
|
|
| 1 |
-
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
if st.session_state.clicked:
|
| 66 |
-
if st.button("Answer"):
|
| 67 |
-
|
| 68 |
-
answer = infer_bot(user_input)
|
| 69 |
-
st.session_state["user"].append(user_input)
|
| 70 |
-
st.session_state["assistant"].append(answer)
|
| 71 |
-
|
| 72 |
-
if st.session_state["assistant"]:
|
| 73 |
-
display_conversation(st.session_state)
|
| 74 |
-
|
| 75 |
if __name__ == "__main__":
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
|
| 4 |
+
client = InferenceClient(model="TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
| 5 |
+
|
| 6 |
+
def respond(message, history, system_message):
|
| 7 |
+
# Mesajları hazırlıyoruz, sistem mesajı ile başlıyoruz
|
| 8 |
+
messages = [{"role": "system", "content": system_message}]
|
| 9 |
+
|
| 10 |
+
for user_msg, assistant_msg in history:
|
| 11 |
+
if user_msg:
|
| 12 |
+
messages.append({"role": "user", "content": user_msg})
|
| 13 |
+
if assistant_msg:
|
| 14 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 15 |
+
|
| 16 |
+
# Son kullanıcı mesajını ekliyoruz
|
| 17 |
+
messages.append({"role": "user", "content": message})
|
| 18 |
+
|
| 19 |
+
response = ""
|
| 20 |
+
try:
|
| 21 |
+
# Yanıtları alıyoruz ve yayınlıyoruz
|
| 22 |
+
result = client.chat_completion(
|
| 23 |
+
messages=messages,
|
| 24 |
+
max_tokens=250,
|
| 25 |
+
temperature=0.7,
|
| 26 |
+
top_p=0.95,
|
| 27 |
+
)
|
| 28 |
+
for choice in result.choices:
|
| 29 |
+
response += choice.message.get('content', '')
|
| 30 |
+
yield response
|
| 31 |
+
except Exception as e:
|
| 32 |
+
# Hata durumunda hata mesajını döndürüyoruz
|
| 33 |
+
yield f"Hata: {e}"
|
| 34 |
+
|
| 35 |
+
# Gradio arayüzünü oluşturuyoruz
|
| 36 |
+
with gr.Blocks(theme=gr.Theme.from_hub('HaleyCH/HaleyCH_Theme')) as demo:
|
| 37 |
+
system_message = gr.HTML("""
|
| 38 |
+
<h1 style="color: #fff; text-shadow: 0 0 5px #fff, 0 0 10px #fff, 0 0 15px #fff, 0 0 10px #0000ff, 0 0 15px #0000ff; text-align: center;">
|
| 39 |
+
SIMULACRA GPT-2
|
| 40 |
+
</h1>
|
| 41 |
+
<p>🤖 Welcome to Simulacra user! See our account for more information.</p>
|
| 42 |
+
""")
|
| 43 |
+
|
| 44 |
+
chatbot = gr.Chatbot()
|
| 45 |
+
msg = gr.Textbox(label="Mesajınızı yazın")
|
| 46 |
|
| 47 |
+
# Butonları yan yana koymak için bir satır içine alıyoruz
|
| 48 |
+
with gr.Row():
|
| 49 |
+
clear = gr.Button("Temizle")
|
| 50 |
+
submit = gr.Button("Gönder")
|
| 51 |
+
|
| 52 |
+
def user_input(user_message, history):
|
| 53 |
+
# Mesajı HTML ile resim ekleyerek döndürme
|
| 54 |
+
user_message_with_image = f'<img src="file_path/favicon.ico" alt="icon" style="width: 16px; height: 16px; vertical-align: middle;"> {user_message}'
|
| 55 |
+
return "", history + [[user_message_with_image, None]]
|
| 56 |
+
|
| 57 |
+
def bot_response(history):
|
| 58 |
+
last_message = history[-1][0]
|
| 59 |
+
response_gen = respond(
|
| 60 |
+
message=last_message,
|
| 61 |
+
history=history[:-1],
|
| 62 |
+
system_message=system_message.value,
|
| 63 |
+
)
|
| 64 |
+
for response in response_gen:
|
| 65 |
+
history[-1][1] = response
|
| 66 |
+
yield history
|
| 67 |
+
|
| 68 |
+
msg.submit(user_input, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 69 |
+
bot_response, chatbot, chatbot
|
| 70 |
+
)
|
| 71 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 72 |
+
submit.click(lambda: msg.submit(), None, chatbot, queue=False) # Gönder butonuna tıklandığında mesajı gönder
|
| 73 |
+
|
| 74 |
+
# Uygulamayı başlatıyoruz
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
if __name__ == "__main__":
|
| 76 |
+
demo.launch(share=True)
|
|
|
|
|
|