Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Untitled0.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1kkl1QEIaAfNoIvtzgEzbQIUhuxSI6FIl
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
pip install -q transformers
|
| 11 |
+
|
| 12 |
+
import gradio as gr
|
| 13 |
+
from transformers import pipeline
|
| 14 |
+
import re
|
| 15 |
+
|
| 16 |
+
# Assuming 'pipe' is already defined as in the previous code snippet
|
| 17 |
+
pipe = pipeline("text-generation", model="ai-forever/mGPT-1.3B-azerbaijan")
|
| 18 |
+
|
| 19 |
+
def chat_with_bot(bot_personality, user_input):
|
| 20 |
+
"""
|
| 21 |
+
Chats with the bot, considering bot personality and providing accurate answers in Azerbaijani.
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
bot_personality: A string describing the bot's personality (e.g., "helpful", "friendly").
|
| 25 |
+
user_input: The user's message.
|
| 26 |
+
|
| 27 |
+
Returns:
|
| 28 |
+
The bot's response.
|
| 29 |
+
"""
|
| 30 |
+
prompt = f"Sən {bot_personality} bir botsan. İstifadəçinin sualına düzgün və ətraflı cavab ver.\nİstifadəçi: {user_input}\nBot:"
|
| 31 |
+
try:
|
| 32 |
+
response = pipe(prompt, max_length=200, num_return_sequences=1)
|
| 33 |
+
match = re.search(r"Bot:\s*(.*?)(?=(İstifadəçi:|[\r\n]*$))", response[0]['generated_text'], re.DOTALL)
|
| 34 |
+
if match:
|
| 35 |
+
bot_message = match.group(1).strip()
|
| 36 |
+
else:
|
| 37 |
+
bot_message = "Üzr istəyirəm, səni başa düşmədim."
|
| 38 |
+
return bot_message
|
| 39 |
+
except:
|
| 40 |
+
print("An error occurred during the chat with the bot.")
|
| 41 |
+
return "Üzr istəyirəm, botla söhbət zamanı xəta baş verdi." # Return error message to Gradio
|
| 42 |
+
|
| 43 |
+
iface = gr.Interface(
|
| 44 |
+
fn=chat_with_bot,
|
| 45 |
+
inputs=[
|
| 46 |
+
gr.inputs.Dropdown(["köməkçi", "dostcasına", "ciddi"], label="Bot Şəxsiyyəti"), # Personality options
|
| 47 |
+
gr.inputs.Textbox(lines=2, placeholder="Sualınızı bura yazın...", label="İstifadəçi Girişi"), # User input
|
| 48 |
+
],
|
| 49 |
+
outputs="text", # Bot response output
|
| 50 |
+
title="Azərbaycanca Söhbət Botu", # Interface title
|
| 51 |
+
description="Bu bot, suallarınıza Azərbaycan dilində cavab vermək üçün hazırlanmışdır.", # Interface description
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
iface.launch(share=True) # Launch the interface and get a shareable link
|