SimrusDenuvo commited on
Commit
a7e2438
·
verified ·
1 Parent(s): 912acb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -18
app.py CHANGED
@@ -1,30 +1,40 @@
1
- import openai
2
- import os
 
 
 
 
 
3
  import gradio as gr
4
 
5
- # Получаем ключ API из переменной окружения
6
- openai.api_key = os.environ.get("OPENAI_API_KEY")
7
-
8
- # Основная функция общения с ChatGPT
9
- def chatgpt_prompt(prompt):
10
- try:
11
- client = openai.OpenAI(api_key=openai.api_key)
12
- response = client.chat.completions.create(
13
- model="gpt-3.5-turbo",
14
- messages=[{"role": "user", "content": prompt}]
15
- )
16
- return response.choices[0].message.content
17
- except Exception as e:
18
- return f"Ошибка ChatGPT: {str(e)}"
 
 
 
 
 
19
 
20
  # Интерфейс Gradio
21
  iface = gr.Interface(
22
- fn=chatgpt_prompt,
23
  inputs=gr.Textbox(label="Введите ваш запрос"),
24
  outputs=gr.Textbox(label="Ответ от ChatGPT"),
25
  title="Интерфейс ChatGPT",
26
  description="Пример взаимодействия с API OpenAI через Hugging Face Space"
27
  )
28
 
29
- # Запуск
30
  iface.launch()
 
 
1
+
2
+ # Запуск
3
+ iface.launch()
4
+
5
+
6
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
7
+ import torch
8
  import gradio as gr
9
 
10
+ # Загружаем токенизатор и модель
11
+ model_id = "HuggingFaceH4/zephyr-7b-beta"
12
+
13
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
14
+ model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
15
+
16
+ # Функция генерации диалогового ответа
17
+ def generate_zephyr_response(prompt):
18
+ messages = [
19
+ {"role": "system", "content": "Ты — дружелюбный и умный ассистент."},
20
+ {"role": "user", "content": prompt}
21
+ ]
22
+ prompt_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
23
+ inputs = tokenizer(prompt_text, return_tensors="pt").to(model.device)
24
+
25
+ with torch.no_grad():
26
+ outputs = model.generate(**inputs, max_new_tokens=256, do_sample=True, temperature=0.7)
27
+ result = tokenizer.decode(outputs[0], skip_special_tokens=True)
28
+ return result.split("assistant")[-1].strip()
29
 
30
  # Интерфейс Gradio
31
  iface = gr.Interface(
32
+ fn=generate_zephyr_response,
33
  inputs=gr.Textbox(label="Введите ваш запрос"),
34
  outputs=gr.Textbox(label="Ответ от ChatGPT"),
35
  title="Интерфейс ChatGPT",
36
  description="Пример взаимодействия с API OpenAI через Hugging Face Space"
37
  )
38
 
 
39
  iface.launch()
40
+