Marek4321 commited on
Commit
fe41d32
verified
1 Parent(s): 57e0696

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -16
app.py CHANGED
@@ -82,34 +82,68 @@ def create_ppt_from_modules(modules, output_file, aspect_ratio="16:9"):
82
  # Funkcja do generowania tre艣ci prezentacji za pomoc膮 OpenAI
83
  def generate_presentation_content_openai(prompt, api_key, progress_bar, progress_text):
84
  try:
85
- # Konfiguracja klienta OpenAI
86
- client = openai.OpenAI(api_key=api_key)
 
 
87
 
88
- # Ustaw streamowanie dla post臋pu
89
- response = client.chat.completions.create(
90
- model="gpt-4o",
91
- messages=[
92
  {"role": "system", "content": "Jeste艣 ekspertem w tworzeniu prezentacji. Tw贸rz tre艣膰 prezentacji w formacie, gdzie ka偶dy slajd zaczyna si臋 od '#### Tytu艂 slajdu' a nast臋pnie zawiera tre艣膰 slajdu. U偶ywaj j臋zyka polskiego."},
93
  {"role": "user", "content": prompt}
94
  ],
 
 
 
 
 
 
 
 
 
95
  stream=True,
96
- max_tokens=8000
 
 
 
 
97
  )
98
 
99
- # Aktualizacja paska post臋pu i wy艣wietlanie generowanej tre艣ci
 
 
 
 
 
100
  full_content = ""
101
- for chunk in response:
102
- if hasattr(chunk.choices[0].delta, 'content') and chunk.choices[0].delta.content:
103
- content_chunk = chunk.choices[0].delta.content
104
- full_content += content_chunk
105
- progress_text.markdown(full_content)
106
- time.sleep(0.01)
107
- # Symulacja post臋pu - nie mo偶emy dok艂adnie wiedzie膰 ile tekstu jeszcze zostanie wygenerowane
108
- progress_bar.progress(min(len(full_content) / 3000, 1.0))
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  progress_bar.progress(1.0)
111
  return full_content
112
 
 
 
 
 
113
  except Exception as e:
114
  logger.error(f"B艂膮d podczas generowania tre艣ci przez OpenAI: {e}")
115
  st.error(f"B艂膮d podczas generowania tre艣ci przez OpenAI: {e}")
 
82
  # Funkcja do generowania tre艣ci prezentacji za pomoc膮 OpenAI
83
  def generate_presentation_content_openai(prompt, api_key, progress_bar, progress_text):
84
  try:
85
+ headers = {
86
+ "Content-Type": "application/json",
87
+ "Authorization": f"Bearer {api_key}"
88
+ }
89
 
90
+ data = {
91
+ "model": "gpt-4o",
92
+ "messages": [
 
93
  {"role": "system", "content": "Jeste艣 ekspertem w tworzeniu prezentacji. Tw贸rz tre艣膰 prezentacji w formacie, gdzie ka偶dy slajd zaczyna si臋 od '#### Tytu艂 slajdu' a nast臋pnie zawiera tre艣膰 slajdu. U偶ywaj j臋zyka polskiego."},
94
  {"role": "user", "content": prompt}
95
  ],
96
+ "stream": True,
97
+ "max_tokens": 8000
98
+ }
99
+
100
+ # Wykonanie 偶膮dania ze streamowaniem
101
+ response = requests.post(
102
+ "https://api.openai.com/v1/chat/completions",
103
+ json=data,
104
+ headers=headers,
105
  stream=True,
106
+ # Mo偶emy doda膰 proxy, je艣li jest to wymagane
107
+ proxies={
108
+ "http": os.getenv("HTTP_PROXY", ""),
109
+ "https": os.getenv("HTTPS_PROXY", "")
110
+ }
111
  )
112
 
113
+ if response.status_code != 200:
114
+ logger.error(f"B艂膮d OpenAI API: {response.status_code} - {response.text}")
115
+ st.error(f"B艂膮d OpenAI API: {response.status_code} - {response.text}")
116
+ return None
117
+
118
+ # Przetwarzanie streamowanej odpowiedzi
119
  full_content = ""
120
+ for line in response.iter_lines():
121
+ if line:
122
+ line = line.decode('utf-8')
123
+ if line.startswith("data:") and not line.startswith("data: [DONE]"):
124
+ try:
125
+ json_str = line[5:].strip()
126
+ if json_str:
127
+ import json
128
+ data = json.loads(json_str)
129
+ if "choices" in data and len(data["choices"]) > 0:
130
+ choice = data["choices"][0]
131
+ if "delta" in choice and "content" in choice["delta"] and choice["delta"]["content"]:
132
+ content_chunk = choice["delta"]["content"]
133
+ full_content += content_chunk
134
+ progress_text.markdown(full_content)
135
+ # Symulacja post臋pu
136
+ progress_bar.progress(min(len(full_content) / 3000, 1.0))
137
+ except Exception as e:
138
+ logger.error(f"B艂膮d podczas parsowania odpowiedzi OpenAI: {e}")
139
 
140
  progress_bar.progress(1.0)
141
  return full_content
142
 
143
+ except RequestException as e:
144
+ logger.error(f"B艂膮d po艂膮czenia z OpenAI API: {e}")
145
+ st.error(f"B艂膮d po艂膮czenia z OpenAI API: {e}")
146
+ return None
147
  except Exception as e:
148
  logger.error(f"B艂膮d podczas generowania tre艣ci przez OpenAI: {e}")
149
  st.error(f"B艂膮d podczas generowania tre艣ci przez OpenAI: {e}")