enotkrutoy commited on
Commit
987b90e
·
verified ·
1 Parent(s): b0c98d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -4,10 +4,8 @@ import gradio as gr
4
  from typing import List, Optional, Tuple, Dict, Iterator
5
 
6
  # 🛠 Безопасная загрузка API-ключа
7
- GROQ_API_KEY = os.getenv("GROQ_API_KEY", "").strip()
8
- if not GROQ_API_KEY:
9
- raise ValueError("API ключ не найден! Укажите его в коде или в переменной окружения.")
10
-
11
  # 🛠 Инициализация клиента
12
  client = OpenAI(
13
  api_key=GROQ_API_KEY,
@@ -32,8 +30,8 @@ def history_to_messages(history: History, system: str) -> Messages:
32
  messages.append({'role': 'assistant', 'content': ai_response})
33
  return messages
34
 
35
- # 🛠 Функция общения с моделью (с исправленным потоком)
36
- def model_chat(query: Optional[str], history: Optional[History], system: str, model_name: str) -> Iterator[Tuple[str, History, str]]:
37
  if not query:
38
  return
39
  if history is None:
@@ -56,12 +54,12 @@ def model_chat(query: Optional[str], history: Optional[History], system: str, mo
56
  content = getattr(delta, "content", "")
57
  if content:
58
  full_response += content
59
- yield full_response, history + [(query, full_response)], system
60
 
61
  except Exception as e:
62
  import traceback
63
  error_message = traceback.format_exc()
64
- yield f"Ошибка: {error_message}", history, system
65
 
66
  # 🛠 Корректное отображение выбора модели
67
  def choose_radio(model_name: str, system: str):
@@ -101,8 +99,8 @@ def main():
101
  clear_history = gr.Button("🧹 Очистить историю")
102
  submit = gr.Button("🚀 Отправить")
103
 
104
- textbox.submit(model_chat, inputs=[textbox, chatbot, system_state, radio], outputs=[textbox, chatbot, system_input])
105
- submit.click(model_chat, inputs=[textbox, chatbot, system_state, radio], outputs=[textbox, chatbot, system_input])
106
  clear_history.click(fn=clear_session, inputs=[], outputs=[textbox, chatbot, system_input])
107
  modify_system.click(fn=modify_system_session, inputs=[system_input], outputs=[system_state, system_input, chatbot])
108
 
@@ -111,5 +109,14 @@ def main():
111
  demo.queue(api_open=False)
112
  demo.launch()
113
 
 
 
 
 
 
 
 
 
114
  if __name__ == "__main__":
115
  main()
 
 
4
  from typing import List, Optional, Tuple, Dict, Iterator
5
 
6
  # 🛠 Безопасная загрузка API-ключа
7
+ #GROQ_API_KEY = "ВАШ_КЛЮЧ" # Укажите API-ключ явно
8
+ GROQ_API_KEY = os.environ.get('GROQ_API_KEY')
 
 
9
  # 🛠 Инициализация клиента
10
  client = OpenAI(
11
  api_key=GROQ_API_KEY,
 
30
  messages.append({'role': 'assistant', 'content': ai_response})
31
  return messages
32
 
33
+ # 🛠 Функция общения с моделью
34
+ def model_chat(query: Optional[str], history: Optional[History], system: str, model_name: str) -> Iterator[List[Dict[str, str]]]:
35
  if not query:
36
  return
37
  if history is None:
 
54
  content = getattr(delta, "content", "")
55
  if content:
56
  full_response += content
57
+ yield [{"role": "assistant", "content": full_response}], history + [(query, full_response)], system
58
 
59
  except Exception as e:
60
  import traceback
61
  error_message = traceback.format_exc()
62
+ yield [{"role": "system", "content": f"Ошибка: {error_message}"}], history, system
63
 
64
  # 🛠 Корректное отображение выбора модели
65
  def choose_radio(model_name: str, system: str):
 
99
  clear_history = gr.Button("🧹 Очистить историю")
100
  submit = gr.Button("🚀 Отправить")
101
 
102
+ textbox.submit(model_chat, inputs=[textbox, chatbot, system_state, radio], outputs=[chatbot, chatbot, system_input])
103
+ submit.click(model_chat, inputs=[textbox, chatbot, system_state, radio], outputs=[chatbot, chatbot, system_input])
104
  clear_history.click(fn=clear_session, inputs=[], outputs=[textbox, chatbot, system_input])
105
  modify_system.click(fn=modify_system_session, inputs=[system_input], outputs=[system_state, system_input, chatbot])
106
 
 
109
  demo.queue(api_open=False)
110
  demo.launch()
111
 
112
+ # Функция теста API
113
+ def test_api():
114
+ chat_completion = client.chat.completions.create(
115
+ model="deepseek-r1-distill-llama-70b",
116
+ messages=[{"role": "user", "content": "What's the purpose of Generative AI?"}],
117
+ )
118
+ print(chat_completion.choices[0].message.content)
119
+
120
  if __name__ == "__main__":
121
  main()
122
+ test_api()