Spaces:
Running
Running
Commit
·
4bd1e00
1
Parent(s):
1fd5432
Update space
Browse files- app.py +31 -59
- models.py +37 -0
- requirements.txt +3 -1
app.py
CHANGED
@@ -1,64 +1,36 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
""
|
5 |
-
|
6 |
-
""
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
-
|
62 |
-
|
63 |
-
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
+
from models import ModelChain
|
2 |
import gradio as gr
|
|
|
3 |
|
4 |
+
DEEPSEEK_MODEL = "deepseek/deepseek-r1:free"
|
5 |
+
GEMINI_MODEL = "google/gemini-2.0-flash-exp:free"
|
6 |
+
QWEN_MODEL="qwen/qwen2.5-vl-72b-instruct:free"
|
7 |
+
|
8 |
+
def get_models_response(models,user_input,system_prompt):
|
9 |
+
if len(models) >1:
|
10 |
+
return "Currently Unsupported"
|
11 |
+
else:
|
12 |
+
print(f"Reponse using model {models}")
|
13 |
+
chain = ModelChain()
|
14 |
+
if models[0]=="deepseek-r1":
|
15 |
+
return chain.get_model_response(DEEPSEEK_MODEL,user_input,system_prompt)
|
16 |
+
elif models[0]=="gemini-2.0-flash-exp":
|
17 |
+
return chain.get_model_response(GEMINI_MODEL,user_input,system_prompt)
|
18 |
+
elif models[0]=="qwen2.5-vl-72b-instruct":
|
19 |
+
return chain.get_model_response(QWEN_MODEL,user_input,system_prompt)
|
20 |
+
else:
|
21 |
+
return "Current Unsupported"
|
22 |
+
|
23 |
+
def main():
|
24 |
+
view = gr.Interface(
|
25 |
+
fn= get_models_response,
|
26 |
+
inputs = [gr.CheckboxGroup(["gemini-2.0-flash-exp","deepseek-r1","qwen2.5-vl-72b-instruct"], label = "Response model", value = "deepseek-r1"),gr.Textbox(label = "Your input",lines = 10, placeholder = "Nhập nội dung"), gr.Textbox(label = "Nhiệm vụ của Bot", placeholder = "Vd: bạn là một chuyên gia thương mại điện tử 10 năm kinh nghiệm hãy giúp tôi trả lời các câu hỏi sau")],
|
27 |
+
outputs = gr.Textbox(label ="Output", lines = 26),
|
28 |
+
flagging_mode = "never",
|
29 |
+
stop_btn = gr.Button("Stop",variant = "stop",visible = True),
|
30 |
+
).launch(share = True)
|
31 |
+
|
32 |
+
if __name__ == '__main__':
|
33 |
+
main()
|
34 |
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
models.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import os
|
3 |
+
import openai
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
# MOELS
|
6 |
+
DEEPSEEK_MODEL = "deepseek/deepseek-r1:free"
|
7 |
+
GEMINI_MODEL = "google/gemini-2.0-flash-exp:free"
|
8 |
+
QWEN_MODEL="qwen/qwen2.5-vl-72b-instruct:free"
|
9 |
+
OPENROUTER_API_KEY="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"
|
10 |
+
load_dotenv()
|
11 |
+
class ModelChain:
|
12 |
+
def __init__(self):
|
13 |
+
self.client = self.generate_client(OPENROUTER_API_KEY,"https://openrouter.ai/api/v1" )
|
14 |
+
self.deepseek_messages = []
|
15 |
+
self.gemini_messages = []
|
16 |
+
|
17 |
+
def generate_client(self,api_key, url):
|
18 |
+
return openai.OpenAI(
|
19 |
+
api_key = api_key,
|
20 |
+
base_url = url,
|
21 |
+
)
|
22 |
+
def get_model_response(self,model,user_input,system_prompt):
|
23 |
+
messages = [{"role":"system","content": system_prompt},
|
24 |
+
{"role":"user","content":user_input}]
|
25 |
+
try:
|
26 |
+
result = self.client.chat.completions.create(
|
27 |
+
model = model,
|
28 |
+
messages = messages,
|
29 |
+
)
|
30 |
+
return result.choices[0].message.content
|
31 |
+
except Exception as e:
|
32 |
+
return f"Error occurred while getting response{e}"
|
33 |
+
def main():
|
34 |
+
chain= Modelschain()
|
35 |
+
|
36 |
+
if __name__ == "__main__":
|
37 |
+
main()
|
requirements.txt
CHANGED
@@ -1 +1,3 @@
|
|
1 |
-
huggingface_hub==0.25.2
|
|
|
|
|
|
1 |
+
huggingface_hub==0.25.2
|
2 |
+
gradio
|
3 |
+
openai
|