Callmebowoo-22 commited on
Commit
2ec2443
·
verified ·
1 Parent(s): 295bd19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -34
app.py CHANGED
@@ -3,18 +3,16 @@ import gradio as gr
3
  from openai import OpenAI
4
  from dotenv import load_dotenv
5
 
6
- # Memuat environment variables
7
  load_dotenv()
8
 
9
  class Chatbot:
10
  def __init__(self):
11
- # Dapatkan API key dari environment variables
12
  self.api_key = os.getenv("OPENROUTER_API_KEY")
13
 
14
  if not self.api_key:
15
  raise ValueError(
16
- "OpenRouter API key not found. "
17
- "Please set it in Hugging Face Secrets or .env file."
18
  )
19
 
20
  self.client = OpenAI(
@@ -22,19 +20,11 @@ class Chatbot:
22
  api_key=self.api_key,
23
  )
24
 
25
- # Konfigurasi model
26
  self.model = "deepseek/deepseek-r1-zero:free"
27
- self.system_prompt = """
28
- Anda adalah asisten AI yang membantu. Berikan jawaban yang:
29
- - Singkat dan jelas
30
- - Ramah dan informatif
31
- - Relevan dengan pertanyaan
32
- """
33
 
34
  def generate_response(self, message, history):
35
- """Generate AI response based on message and conversation history"""
36
  try:
37
- # Format conversation history
38
  messages = [{"role": "system", "content": self.system_prompt}]
39
 
40
  for i, h in enumerate(history):
@@ -45,7 +35,6 @@ class Chatbot:
45
 
46
  messages.append({"role": "user", "content": message})
47
 
48
- # Kirim permintaan ke API
49
  completion = self.client.chat.completions.create(
50
  extra_headers={
51
  "HTTP-Referer": "https://huggingface.co/spaces",
@@ -60,10 +49,9 @@ class Chatbot:
60
  return completion.choices[0].message.content
61
 
62
  except Exception as e:
63
- print(f"Error generating response: {str(e)}")
64
- return f"Maaf, terjadi error: {str(e)}"
65
 
66
- # Inisialisasi chatbot
67
  try:
68
  chatbot = Chatbot()
69
  except ValueError as e:
@@ -72,19 +60,16 @@ except ValueError as e:
72
 
73
  def respond(message, history):
74
  if not chatbot:
75
- return "Error: Chatbot tidak dapat diinisialisasi. Periksa konfigurasi API key."
76
-
77
  return chatbot.generate_response(message, history)
78
 
79
- # Contoh pertanyaan
80
  examples = [
81
- "Apa itu kecerdasan buatan?",
82
- "Buatkan puisi tentang teknologi",
83
- "Jelaskan teori relativitas dengan sederhana",
84
- "Apa kelebihan model DeepSeek?"
85
  ]
86
 
87
- # Buat antarmuka Gradio
88
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
89
  gr.Markdown("""
90
  # 🤖 DeepSeek R1 Zero Chatbot
@@ -93,21 +78,18 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
93
 
94
  with gr.Row():
95
  with gr.Column():
 
96
  gr.ChatInterface(
97
  respond,
98
- examples=examples,
99
- retry_btn="Coba Lagi",
100
- undo_btn="Undo",
101
- clear_btn="Bersihkan"
102
  )
103
  with gr.Column():
104
  gr.Markdown("""
105
  ### Tentang Chatbot Ini
106
- - Menggunakan model **DeepSeek R1 Zero**
107
- - Diakses melalui **OpenRouter API**
108
- - Dibangun dengan **Gradio** di Hugging Face Spaces
109
-
110
- **Catatan**: Chatbot ini hanya untuk demonstrasi.
111
  """)
112
 
113
  if __name__ == "__main__":
 
3
  from openai import OpenAI
4
  from dotenv import load_dotenv
5
 
 
6
  load_dotenv()
7
 
8
  class Chatbot:
9
  def __init__(self):
 
10
  self.api_key = os.getenv("OPENROUTER_API_KEY")
11
 
12
  if not self.api_key:
13
  raise ValueError(
14
+ "OpenRouter API key tidak ditemukan. " +
15
+ "Harap set di Hugging Face Secrets atau file .env"
16
  )
17
 
18
  self.client = OpenAI(
 
20
  api_key=self.api_key,
21
  )
22
 
 
23
  self.model = "deepseek/deepseek-r1-zero:free"
24
+ self.system_prompt = "Anda adalah asisten AI yang membantu dan ramah."
 
 
 
 
 
25
 
26
  def generate_response(self, message, history):
 
27
  try:
 
28
  messages = [{"role": "system", "content": self.system_prompt}]
29
 
30
  for i, h in enumerate(history):
 
35
 
36
  messages.append({"role": "user", "content": message})
37
 
 
38
  completion = self.client.chat.completions.create(
39
  extra_headers={
40
  "HTTP-Referer": "https://huggingface.co/spaces",
 
49
  return completion.choices[0].message.content
50
 
51
  except Exception as e:
52
+ print(f"Error: {str(e)}")
53
+ return f"Maaf, terjadi kesalahan: {str(e)}"
54
 
 
55
  try:
56
  chatbot = Chatbot()
57
  except ValueError as e:
 
60
 
61
  def respond(message, history):
62
  if not chatbot:
63
+ return "Error: Inisialisasi chatbot gagal. Periksa konfigurasi API key."
 
64
  return chatbot.generate_response(message, history)
65
 
 
66
  examples = [
67
+ "Apa itu machine learning?",
68
+ "Buatkan cerpen tentang robot",
69
+ "Jelaskan hukum termodinamika pertama",
70
+ "Apa perbedaan AI dan manusia?"
71
  ]
72
 
 
73
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
74
  gr.Markdown("""
75
  # 🤖 DeepSeek R1 Zero Chatbot
 
78
 
79
  with gr.Row():
80
  with gr.Column():
81
+ # Perbaikan: Menghapus parameter yang tidak didukung
82
  gr.ChatInterface(
83
  respond,
84
+ examples=examples
 
 
 
85
  )
86
  with gr.Column():
87
  gr.Markdown("""
88
  ### Tentang Chatbot Ini
89
+ - **Model**: DeepSeek R1 Zero
90
+ - **Platform**: OpenRouter API
91
+ - **Framework**: Gradio
92
+ - **Host**: Hugging Face Spaces
 
93
  """)
94
 
95
  if __name__ == "__main__":