Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -79,33 +79,98 @@
|
|
| 79 |
# )
|
| 80 |
# iface.launch()
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
import gradio as gr
|
| 83 |
-
|
|
|
|
|
|
|
| 84 |
import time
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
def generate_response(message, chat_history):
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
)
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
time.sleep(0.1)
|
| 98 |
-
|
| 99 |
-
yield chat_history + [(message,
|
|
|
|
| 100 |
|
| 101 |
|
| 102 |
with gr.Blocks() as demo:
|
| 103 |
-
gr.Markdown("<h1 style='text-align: center;'>💬 Parviz
|
| 104 |
|
| 105 |
chatbot = gr.Chatbot(label="جواب")
|
| 106 |
-
msg = gr.Textbox(label="ورودی", placeholder="
|
| 107 |
|
| 108 |
msg.submit(generate_response, [msg, chatbot], chatbot)
|
| 109 |
|
| 110 |
clear = gr.ClearButton([msg, chatbot])
|
|
|
|
| 111 |
demo.launch()
|
|
|
|
|
|
| 79 |
# )
|
| 80 |
# iface.launch()
|
| 81 |
|
| 82 |
+
# import gradio as gr
|
| 83 |
+
# from groq import Groq
|
| 84 |
+
# import time
|
| 85 |
+
|
| 86 |
+
# client = Groq(api_key="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")
|
| 87 |
+
|
| 88 |
+
# def generate_response(message, chat_history):
|
| 89 |
+
# chat_completion = client.chat.completions.create(
|
| 90 |
+
# messages=[{"role": "user", "content": message}],
|
| 91 |
+
# model="llama3-8b-8192",
|
| 92 |
+
# )
|
| 93 |
+
# bot_message = chat_completion.choices[0].message.content
|
| 94 |
+
|
| 95 |
+
# for i in range(0, len(bot_message), 10):
|
| 96 |
+
# yield chat_history + [(message, bot_message[:i + 10])]
|
| 97 |
+
# time.sleep(0.1)
|
| 98 |
+
|
| 99 |
+
# yield chat_history + [(message, bot_message)]
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
# with gr.Blocks() as demo:
|
| 103 |
+
# gr.Markdown("<h1 style='text-align: center;'>💬 Parviz Chatbot</h1><p style='text-align: center; color: #e0e0e0;'>زنده باد</p>")
|
| 104 |
+
|
| 105 |
+
# chatbot = gr.Chatbot(label="جواب")
|
| 106 |
+
# msg = gr.Textbox(label="ورودی", placeholder="اینجا یه چی بپرس... ", lines=1)
|
| 107 |
+
|
| 108 |
+
# msg.submit(generate_response, [msg, chatbot], chatbot)
|
| 109 |
+
|
| 110 |
+
# clear = gr.ClearButton([msg, chatbot])
|
| 111 |
+
# demo.launch()
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
|
| 120 |
import gradio as gr
|
| 121 |
+
import torch
|
| 122 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, GenerationConfig
|
| 123 |
+
import re
|
| 124 |
import time
|
| 125 |
|
| 126 |
+
|
| 127 |
+
tokenizer = AutoTokenizer.from_pretrained("universitytehran/PersianMind-v1.0")
|
| 128 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("universitytehran/PersianMind-v1.0")
|
| 129 |
+
|
| 130 |
|
| 131 |
def generate_response(message, chat_history):
|
| 132 |
+
|
| 133 |
+
TEMPLATE = "{context}\nYou: {prompt}\nParvizGPT "
|
| 134 |
+
CONTEXT = "This is a conversation with ParvizGPT. It is an artificial intelligence model designed by Amir Mahdi Parviz " \
|
| 135 |
+
"NLP expert to help you with various tasks such as answering questions, " \
|
| 136 |
+
"providing recommendations, and helping with decision making. You can ask it anything you want and " \
|
| 137 |
+
"it will do its best to give you accurate and relevant information."
|
| 138 |
+
|
| 139 |
+
prompt = TEMPLATE.format(context=CONTEXT, prompt=message)
|
| 140 |
+
|
| 141 |
+
generation_config = GenerationConfig(
|
| 142 |
+
max_new_tokens=128,
|
| 143 |
+
do_sample=True,
|
| 144 |
+
top_k=50,
|
| 145 |
+
top_p=0.95,
|
| 146 |
+
temperature=0.8,
|
| 147 |
+
repetition_penalty=1.2
|
| 148 |
)
|
| 149 |
+
|
| 150 |
+
tokenized_test_text = tokenizer(prompt, return_tensors='pt').input_ids.to("cpu")
|
| 151 |
+
model.to("cpu")
|
| 152 |
|
| 153 |
+
|
| 154 |
+
outputs = model.generate(tokenized_test_text, generation_config=generation_config, max_new_tokens=128)
|
| 155 |
+
result = tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
| 156 |
+
|
| 157 |
+
for i in range(0, len(result), 10):
|
| 158 |
+
yield chat_history + [(message, result[:i + 10])]
|
| 159 |
time.sleep(0.1)
|
| 160 |
+
|
| 161 |
+
yield chat_history + [(message, result)]
|
| 162 |
+
|
| 163 |
|
| 164 |
|
| 165 |
with gr.Blocks() as demo:
|
| 166 |
+
gr.Markdown("<h1 style='text-align: center;'>💬 Parviz GPT</h1><p style='text-align: center;'>made by A.M.Parviz \</p>")
|
| 167 |
|
| 168 |
chatbot = gr.Chatbot(label="جواب")
|
| 169 |
+
msg = gr.Textbox(label="ورودی", placeholder="سوال خودتو رو بپرس", lines=1)
|
| 170 |
|
| 171 |
msg.submit(generate_response, [msg, chatbot], chatbot)
|
| 172 |
|
| 173 |
clear = gr.ClearButton([msg, chatbot])
|
| 174 |
+
|
| 175 |
demo.launch()
|
| 176 |
+
|