Spaces:
Running
Running
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|