File size: 2,711 Bytes
a11b57b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

# import gradio as gr
# from groq import Groq

# client = Groq(
#     api_key=("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"),
# )

# def generate_response(input_text):
#     chat_completion = client.chat.completions.create(
#         messages=[
#             {
#                 "role": "user",
#                 "content": input_text,
#             }
#         ],
#         model="llama3-8b-8192",
#     )
#     return chat_completion.choices[0].message.content

# custom_css = """
# body {
#     background-color: #f5f5f5;
#     font-family: 'Arial', sans-serif;
#     color: #333;
# }

# .gradio-container {
#     border-radius: 12px;
#     padding: 20px;
#     background-color: #ffffff;
#     box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
# }

# input[type="text"], textarea {
#     border-radius: 10px;
#     border: 1px solid #ddd;
#     padding: 12px;
#     width: 100%;
#     font-size: 14px;
#     color: #333;
#     background-color: #f9f9f9;
# }

# button {
#     background-color: #007bff;
#     color: white;
#     border: none;
#     padding: 12px 24px;
#     border-radius: 10px;
#     cursor: pointer;
#     font-size: 14px;
#     font-weight: bold;
# }

# button:hover {
#     background-color: #0056b3;
# }

# h1 {
#     font-weight: 600;
#     color: #333;
# }

# textarea {
#     resize: none;
# }
# """

# iface = gr.Interface(
#     fn=generate_response,
#     inputs=gr.Textbox(label="ورودی" , lines=2, placeholder="اینجا یه چی بپرس... "),
#     outputs=gr.Textbox(label="جواب"),
#     title="💬 Parviz Chatbot",
#     description="زنده باد",
#     theme="dark",
#     allow_flagging="never"

# )
# iface.launch()

import gradio as gr
from groq import Groq
import time

client = Groq(api_key="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")

def generate_response(message, chat_history):
    chat_completion = client.chat.completions.create(
        messages=[{"role": "user", "content": message}],
        model="llama3-8b-8192",
    )
    bot_message = chat_completion.choices[0].message.content
    
    for i in range(0, len(bot_message), 10):
        yield chat_history + [(message, bot_message[:i + 10])]
        time.sleep(0.1)
    
    yield chat_history + [(message, bot_message)]


with gr.Blocks() as demo:
    gr.Markdown("<h1 style='text-align: center;'>💬 Parviz Chatbot</h1><p style='text-align: center; color: #e0e0e0;'>زنده باد</p>")
    
    chatbot = gr.Chatbot(label="جواب")
    msg = gr.Textbox(label="ورودی", placeholder="اینجا یه چی بپرس... ", lines=1)
    
    msg.submit(generate_response, [msg, chatbot], chatbot)
    
    clear = gr.ClearButton([msg, chatbot])
demo.launch()