Spaces:
Sleeping
Sleeping
File size: 5,055 Bytes
7fe0df9 b001e93 859b0d0 3e5f6ae 859b0d0 3d19ff4 61f51f1 3d19ff4 61f51f1 3d19ff4 61f51f1 3d19ff4 c657076 3d19ff4 61f51f1 3d19ff4 3e5f6ae 859b0d0 cf978fc 0f20fb4 859b0d0 16126d1 09e6a56 16126d1 09e6a56 16126d1 31773d9 7a6311a 0f20fb4 61f51f1 16126d1 075959d 7740cf7 7fe0df9 d6e1127 7fe0df9 e7459df ce7981d c263170 8d8c1ce |
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# app.py
import gradio as gr # type: ignore
import os
# import openai # type: ignore
# # openai.api_key = os.getenv("OPENAI_API_KEY")
# client = openai.OpenAI()
# def respond(
# message,
# history: list[tuple[str, str]],
# system_message,
# max_tokens,
# temperature,
# top_p,
# image_uploaded,
# file_uploaded
# ):
# #read system message
# messages = [{"role": "system", "content": system_message}]
# #read history
# for val in history:
# if val[0]:
# messages.append({"role": "user", "content": val[0]})
# if val[1]:
# messages.append({"role": "assistant", "content": val[1]})
# #read output
# messages.append({"role": "user", "content": message})
# print("## Messages: \n", messages) #debug output
# #create output
# response = client.responses.create(
# model="gpt-4.1-nano",
# input=messages,
# temperature=temperature,
# top_p=top_p,
# max_output_tokens=max_tokens
# )
# #read output
# response = response.output_text
# print("## Response: ", response) #debug output
# print("\n")
# yield response #chat reply
# import torch
# from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
# model_name = "deepseek-ai/deepseek-math-7b-base"
# tokenizer = AutoTokenizer.from_pretrained(model_name)
# model = AutoModelForCausalLM.from_pretrained(model_name)
# # model.generation_config = GenerationConfig.from_pretrained(model_name)
# # model.generation_config.pad_token_id = model.generation_config.eos_token_id
# def deepseek(
# message,
# history: list[tuple[str, str]],
# system_message,
# max_tokens,
# temperature,
# top_p):
# # messages = [
# # {"role": "user", "content": "what is the integral of x^2 from 0 to 2?\nPlease reason step by step, and put your final answer within \\boxed{}."}
# # ]
# messages = [
# {"role": "user", "content": message}
# ]
# input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
# outputs = model.generate(input_tensor.to(model.device), max_new_tokens=100)
# print(outputs)
# print("\n")
# result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
# print(result)
# return result
# import replicate
# def deepseek_api_replicate(
# user_message,
# history: list[tuple[str, str]],
# system_message,
# max_new_tokens,
# temperature,
# top_p):
# """
# Gọi DeepSeek Math trên Replicate và trả ngay kết quả.
# Trả về:
# str hoặc [bytes]: output model sinh ra
# """
# # 1. Khởi tạo client và xác thực
# # token = os.getenv("REPLICATE_API_TOKEN")
# # if not token:
# # raise RuntimeError("Missing REPLICATE_API_TOKEN") # bảo mật bằng biến môi trường
# client = replicate.Client(api_token="REPLICATE_API_TOKEN")
# # 2. Gọi model
# output = client.run(
# "deepseek-ai/deepseek-math-7b-base:61f572dae0985541cdaeb4a114fd5d2d16cb40dac3894da10558992fc60547c7",
# input={
# "system_prompt": system_message,
# "user_prompt": user_message,
# "max_new_tokens": max_new_tokens,
# "temperature": temperature,
# "top_p": top_p
# }
# )
# # 3. Trả kết quả
# return output
import call_api
def respond(
message,
history: list[tuple[str, str]],
system_message,
max_tokens,
temperature,
top_p
):
return call_api.respond(message, history, system_message, max_tokens, temperature)
chat = gr.ChatInterface(
respond, #chat
# title="Trợ lý Học Tập AI",
# description="Nhập câu hỏi của bạn về Toán, Lý, Hóa, Văn… và nhận giải đáp chi tiết ngay lập tức!",
additional_inputs=[
gr.Textbox("Bạn là một chatbot tiếng Việt thân thiện.", label="System message"),
gr.Slider(1, 2048, value=200, step=1, label="Max new tokens"),
gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature"),
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
# gr.Image(type="pil", label="Attach an image (optional)"),
# gr.File(label="Upload a file (optional)"),
]
# examples=[
# # Mỗi item: [message, system_message, max_tokens, temperature, top_p]
# ["tích phân của x^2 từ 0 đến 2 là gì? vui lòng lập luận từng bước, và đặt kết quả cuối cùng trong \boxed{}", "bạn là nhà toán học", 100, 0.7, 0.95],
# ],
)
if __name__ == "__main__":
chat.launch()
|