GIGAParviz commited on
Commit
482b0de
·
verified ·
1 Parent(s): 1ee76ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -158
app.py CHANGED
@@ -1,176 +1,38 @@
1
-
2
- # import gradio as gr
3
- # from groq import Groq
4
-
5
- # client = Groq(
6
- # api_key=("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"),
7
- # )
8
-
9
- # def generate_response(input_text):
10
- # chat_completion = client.chat.completions.create(
11
- # messages=[
12
- # {
13
- # "role": "user",
14
- # "content": input_text,
15
- # }
16
- # ],
17
- # model="llama3-8b-8192",
18
- # )
19
- # return chat_completion.choices[0].message.content
20
-
21
- # custom_css = """
22
- # body {
23
- # background-color: #f5f5f5;
24
- # font-family: 'Arial', sans-serif;
25
- # color: #333;
26
- # }
27
-
28
- # .gradio-container {
29
- # border-radius: 12px;
30
- # padding: 20px;
31
- # background-color: #ffffff;
32
- # box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
33
- # }
34
-
35
- # input[type="text"], textarea {
36
- # border-radius: 10px;
37
- # border: 1px solid #ddd;
38
- # padding: 12px;
39
- # width: 100%;
40
- # font-size: 14px;
41
- # color: #333;
42
- # background-color: #f9f9f9;
43
- # }
44
-
45
- # button {
46
- # background-color: #007bff;
47
- # color: white;
48
- # border: none;
49
- # padding: 12px 24px;
50
- # border-radius: 10px;
51
- # cursor: pointer;
52
- # font-size: 14px;
53
- # font-weight: bold;
54
- # }
55
-
56
- # button:hover {
57
- # background-color: #0056b3;
58
- # }
59
-
60
- # h1 {
61
- # font-weight: 600;
62
- # color: #333;
63
- # }
64
-
65
- # textarea {
66
- # resize: none;
67
- # }
68
- # """
69
-
70
- # iface = gr.Interface(
71
- # fn=generate_response,
72
- # inputs=gr.Textbox(label="ورودی" , lines=2, placeholder="اینجا یه چی بپرس... "),
73
- # outputs=gr.Textbox(label="جواب"),
74
- # title="💬 Parviz Chatbot",
75
- # description="زنده باد",
76
- # theme="dark",
77
- # allow_flagging="never"
78
-
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
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from groq import Groq
 
 
3
  import time
4
 
5
+ client = Groq(api_key="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")
6
 
7
+ CONTEXT = (
8
+ "This is a conversation with ParvizGPT. It is an artificial intelligence model designed by Amir Mahdi Parviz, "
9
+ "an NLP expert, to help you with various tasks such as answering questions in persian, "
10
+ "providing recommendations, and assisting with decision-making. Ask it anything!"
11
+ )
12
 
13
  def generate_response(message, chat_history):
14
+ full_message = CONTEXT + f"\nYou: {message}به فارسی بگو\nParvizGPT: "
 
 
 
 
 
15
 
16
+ chat_completion = client.chat.completions.create(
17
+ messages=[{"role": "user", "content": full_message}],
18
+ model= "llama-3.1-8b-instant",
 
 
 
 
 
 
19
  )
20
+ bot_message = chat_completion.choices[0].message.content
 
 
21
 
22
+ for i in range(0, len(bot_message), 10):
23
+ yield chat_history + [(message, bot_message[:i + 10])]
 
 
 
 
24
  time.sleep(0.1)
25
+
26
+ yield chat_history + [(message, bot_message)]
 
 
27
 
28
  with gr.Blocks() as demo:
29
+ gr.Markdown("<h1 style='text-align: center;'>💬 Parviz GPT</h1><p style='text-align: center; color: #e0e0e0;'>زنده باد</p>")
30
 
31
  chatbot = gr.Chatbot(label="جواب")
32
+ msg = gr.Textbox(label="ورودی", placeholder="اینجا یه چی بپرس... ", lines=1)
33
 
34
  msg.submit(generate_response, [msg, chatbot], chatbot)
35
 
36
  clear = gr.ClearButton([msg, chatbot])
37
+
38
+ demo.launch()