seawolf2357 commited on
Commit
f604197
Β·
verified Β·
1 Parent(s): 5a6859f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -8
app.py CHANGED
@@ -7,23 +7,33 @@ openai.api_key = os.getenv("OPENAI_API_KEY")
7
 
8
  def generate_keyword_from_text(input_text):
9
  try:
10
- # OpenAI ChatCompletion 을 μ‚¬μš©ν•˜μ—¬ μ±— λͺ¨λΈ μš”μ²­
11
  response = openai.ChatCompletion.create(
12
- model="gpt-3.5-turbo",
13
- messages=[
14
- {"role": "user", "content": f"λ‹€μŒ ν…μŠ€νŠΈμ— λŒ€ν•œ Pexels 검색을 μœ„ν•œ 영문 ν‚€μ›Œλ“œ 생성: '{input_text}'"}
15
- ]
 
16
  )
17
- keyword = response['choices'][0]['message']['content']
 
 
 
 
 
 
 
 
 
18
  return keyword
19
  except Exception as e:
20
  print(f"μ—λŸ¬ λ°œμƒ: {e}")
21
- return "ν‚€μ›Œλ“œ 생성 쀑 μ—λŸ¬ λ°œμƒ"
22
 
23
  # Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •κ³Ό μ‹€ν–‰
24
  iface = gr.Interface(
25
  fn=generate_keyword_from_text,
26
- inputs=gr.Textbox(lines=2, label="ν…μŠ€νŠΈ μž…λ ₯"),
27
  outputs="text",
28
  title="GPTλ₯Ό μ΄μš©ν•œ Pexels 검색 ν‚€μ›Œλ“œ 생성",
29
  description="제곡된 ν…μŠ€νŠΈλ₯Ό λ°”νƒ•μœΌλ‘œ Pexels 검색에 μ‚¬μš©ν•  영문 ν‚€μ›Œλ“œλ₯Ό μžλ™ μƒμ„±ν•©λ‹ˆλ‹€."
 
7
 
8
  def generate_keyword_from_text(input_text):
9
  try:
10
+ # OpenAI ChatCompletion 을 μ‚¬μš©ν•˜μ—¬ ν•œ μ€„μ˜ ν‚€μ›Œλ“œ μš”μ²­
11
  response = openai.ChatCompletion.create(
12
+ model="gpt-3.5-turbo",
13
+ messages=[{
14
+ 'role': 'user',
15
+ 'content': f"λ‹€μŒ ν…μŠ€νŠΈλ₯Ό λ°”νƒ•μœΌλ‘œ Pexels μ—μ„œ 잘 검색될 영문 ν‚€μ›Œλ“œ ν•œ 쀄을 μƒμ„±ν•˜μ„Έμš”: '{input_text}'"
16
+ }]
17
  )
18
+ # 첫 번째 choiceμ—μ„œ message contentλ₯Ό λ°›μ•„μ˜΄
19
+ keyword_full_response = response['choices'][0]['message']['content']
20
+
21
+ # κ°€μž₯ 첫 번째 라인만 μΆ”μΆœ
22
+ keyword = keyword_full_response.split('\n', 1)[0].strip()
23
+
24
+ # '""'둜 감싼 ν‚€μ›Œλ“œ 제거
25
+ if keyword.startswith('"') and keyword.endswith('"'):
26
+ keyword = keyword[1:-1].strip()
27
+
28
  return keyword
29
  except Exception as e:
30
  print(f"μ—λŸ¬ λ°œμƒ: {e}")
31
+ return f"ν‚€μ›Œλ“œ 생성 쀑 μ—λŸ¬ λ°œμƒ: {e}"
32
 
33
  # Gradio μΈν„°νŽ˜μ΄μŠ€ μ„€μ •κ³Ό μ‹€ν–‰
34
  iface = gr.Interface(
35
  fn=generate_keyword_from_text,
36
+ inputs=gr.Textbox(lines=2, placeholder="여기에 Pexels 검색을 μœ„ν•œ ν…μŠ€νŠΈλ₯Ό μž…λ ₯ν•˜μ„Έμš”."),
37
  outputs="text",
38
  title="GPTλ₯Ό μ΄μš©ν•œ Pexels 검색 ν‚€μ›Œλ“œ 생성",
39
  description="제곡된 ν…μŠ€νŠΈλ₯Ό λ°”νƒ•μœΌλ‘œ Pexels 검색에 μ‚¬μš©ν•  영문 ν‚€μ›Œλ“œλ₯Ό μžλ™ μƒμ„±ν•©λ‹ˆλ‹€."