|
import gradio as gr |
|
import requests |
|
from huggingface_hub import InferenceClient |
|
|
|
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") |
|
|
|
GITHUB_RAW_URL = "https://raw.githubusercontent.com/ALPERALL/AlpDroid/main/prompt.txt" |
|
|
|
def fetch_system_message(): |
|
try: |
|
response = requests.get(GITHUB_RAW_URL) |
|
response.raise_for_status() |
|
return response.text.strip() |
|
except requests.exceptions.RequestException as e: |
|
return f"Error fetching system message: {str(e)}" |
|
|
|
def respond(message, history): |
|
max_tokens = 512 |
|
temperature = 0.7 |
|
top_p = 0.95 |
|
|
|
system_message = fetch_system_message() |
|
if system_message.startswith("Error"): |
|
yield system_message |
|
return |
|
|
|
messages = [{"role": "system", "content": system_message}] |
|
for val in history: |
|
if val[0]: |
|
messages.append({"role": "user", "content": val[0]}) |
|
if val[1]: |
|
messages.append({"role": "assistant", "content": val[1]}) |
|
|
|
messages.append({"role": "user", "content": message}) |
|
response = "" |
|
|
|
for message in client.chat_completion( |
|
messages, |
|
max_tokens=max_tokens, |
|
stream=True, |
|
temperature=temperature, |
|
top_p=top_p, |
|
): |
|
token = message.choices[0].delta.content |
|
response += token |
|
yield response |
|
|
|
theme = gr.themes.Soft( |
|
primary_hue="purple", |
|
secondary_hue="purple", |
|
neutral_hue="gray", |
|
font=[ |
|
gr.themes.GoogleFont("Exo"), |
|
"ui-sans-serif", |
|
"system-ui", |
|
"sans-serif" |
|
] |
|
).set( |
|
body_background_fill_dark="#010409", |
|
block_background_fill_dark="#010409", |
|
block_border_width="1px", |
|
block_title_background_fill_dark="#1e1c26", |
|
input_background_fill_dark="#161b22", |
|
button_secondary_background_fill_dark="#21262d", |
|
border_color_accent_dark="#2f353c", |
|
border_color_primary_dark="#2f353c", |
|
background_fill_secondary_dark="#010409", |
|
color_accent_soft_dark="transparent", |
|
code_background_fill_dark="#0d1117", |
|
) |
|
|
|
css = """ |
|
#input-textbox textarea { |
|
height: 50px !important; |
|
border-radius: 10px 0 0 10px !important; |
|
padding: 10px !important; |
|
font-size: 16px !important; |
|
resize: none !important; |
|
} |
|
|
|
#submit-button { |
|
width: 50px !important; |
|
height: 50px !important; |
|
border-radius: 0 10px 10px 0 !important; |
|
font-size: 24px !important; |
|
padding: 0 !important; |
|
} |
|
""" |
|
|
|
with gr.Blocks(theme=theme, css=css) as demo: |
|
gr.Markdown("## 🤖 AlpDroid\nAlper A. tarafından yapıldı, yasadışı kullanımda sorumluluk kullanıcıya aittir.") |
|
|
|
chatbot = gr.Chatbot(label="AlpDroid") |
|
state = gr.State([]) |
|
|
|
with gr.Row(): |
|
txt = gr.Textbox( |
|
show_label=False, |
|
placeholder="Sohbet etmek için dokun...", |
|
scale=4, |
|
elem_id="input-textbox" |
|
) |
|
submit = gr.Button("⬆️", scale=1, elem_id="submit-button") |
|
|
|
with gr.Row(): |
|
file_upload = gr.File(label="Dosya Yükle", file_types=["image", "video", ".pdf", ".txt"], file_count="single") |
|
|
|
def user_submit(message, file, history): |
|
if file is not None: |
|
message = f"Kullanıcı bir dosya gönderdi: {file.name}\nMesaj: {message}" |
|
|
|
history = history or [] |
|
response_gen = respond(message, history) |
|
response = "" |
|
for token in response_gen: |
|
response = token |
|
history.append((message, response)) |
|
return history, "", None |
|
|
|
submit.click(fn=user_submit, inputs=[txt, file_upload, state], outputs=[chatbot, txt, file_upload]) |
|
txt.submit(fn=user_submit, inputs=[txt, file_upload, state], outputs=[chatbot, txt, file_upload]) |
|
|
|
if __name__ == "__main__": |
|
demo.launch(share=True) |