820nam commited on
Commit
7c4d402
ยท
verified ยท
1 Parent(s): 289d3ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -28
app.py CHANGED
@@ -2,26 +2,11 @@ import streamlit as st
2
  import requests
3
  import matplotlib.pyplot as plt
4
  from transformers import pipeline
5
- from openai import OpenAI # 1.0.0+์—์„œ ์ƒˆ๋กœ์šด ํด๋ผ์ด์–ธํŠธ ๋ฐฉ์‹
6
  import os
7
 
8
- # Hugging Face Secrets์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
9
- # `OPENAI_API_KEY` ํ™˜๊ฒฝ ๋ณ€์ˆ˜์— API ํ‚ค๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
10
- api_key = os.getenv("OPENAI_API_KEY") # Secrets์—์„œ API ํ‚ค ๊ฐ€์ ธ์˜ค๊ธฐ
11
-
12
- # OpenAI ํด๋ผ์ด์–ธํŠธ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
13
- client = OpenAI(api_key=api_key) # API ํ‚ค๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌ
14
-
15
- # ํด๋ผ์ด์–ธํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ OpenAI API ํ˜ธ์ถœ
16
- def test_openai_api():
17
- try:
18
- completion = client.completions.create(
19
- model="gpt-4",
20
- messages=[{"role": "user", "content": "Hello world"}]
21
- )
22
- return completion.choices[0].text
23
- except Exception as e:
24
- return f"Error: {e}"
25
 
26
  # ๋„ค์ด๋ฒ„ ๋‰ด์Šค API๋ฅผ ํ†ตํ•ด ์‹ค์ œ ๋‰ด์Šค ๊ธฐ์‚ฌ ๊ฐ€์ ธ์˜ค๊ธฐ
27
  def fetch_naver_news(query, display=5):
@@ -53,21 +38,20 @@ def load_sentiment_model():
53
  classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
54
  return classifier
55
 
56
- # GPT-4๋ฅผ ์ด์šฉํ•ด ๋ฐ˜๋Œ€ ๊ด€์  ๊ธฐ์‚ฌ ์ƒ์„ฑ
57
  # GPT-4๋ฅผ ์ด์šฉํ•ด ๋ฐ˜๋Œ€ ๊ด€์  ๊ธฐ์‚ฌ ์ƒ์„ฑ
58
  def generate_article_gpt4(prompt):
59
  try:
60
- # OpenAI ํด๋ผ์ด์–ธํŠธ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
61
- client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
62
-
63
- # GPT-4๋กœ ๋ฐ˜๋Œ€ ๊ด€์  ๊ธฐ์‚ฌ ์ƒ์„ฑ
64
- response = client.completions.create(
65
- model="gpt-4", # ๋ชจ๋ธ ์ง€์ •
66
- prompt=prompt, # ํ”„๋กฌํ”„ํŠธ ์ง€์ •
67
  max_tokens=512, # ์ƒ์„ฑํ•  ํ…์ŠคํŠธ์˜ ์ตœ๋Œ€ ๊ธธ์ด
68
- temperature=0.7 # ์ถœ๋ ฅ์˜ ์ฐฝ์˜์„ฑ ์ •๋„
69
  )
70
- return response.choices[0].text # GPT์˜ ์‘๋‹ต ํ…์ŠคํŠธ
71
  except Exception as e:
72
  return f"Error generating text: {e}"
73
 
 
2
  import requests
3
  import matplotlib.pyplot as plt
4
  from transformers import pipeline
5
+ import openai
6
  import os
7
 
8
+ # OpenAI API ํ‚ค ์„ค์ • (ํ™˜๊ฒฝ๋ณ€์ˆ˜์—์„œ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•)
9
+ openai.api_key = os.getenv("OPENAI_API_KEY")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  # ๋„ค์ด๋ฒ„ ๋‰ด์Šค API๋ฅผ ํ†ตํ•ด ์‹ค์ œ ๋‰ด์Šค ๊ธฐ์‚ฌ ๊ฐ€์ ธ์˜ค๊ธฐ
12
  def fetch_naver_news(query, display=5):
 
38
  classifier = pipeline("text-classification", model="bucketresearch/politicalBiasBERT")
39
  return classifier
40
 
 
41
  # GPT-4๋ฅผ ์ด์šฉํ•ด ๋ฐ˜๋Œ€ ๊ด€์  ๊ธฐ์‚ฌ ์ƒ์„ฑ
42
  def generate_article_gpt4(prompt):
43
  try:
44
+ # GPT-4 ๋ชจ๋ธ์„ ์ด์šฉํ•ด ๋ฐ˜๋Œ€ ๊ด€์  ๊ธฐ์‚ฌ๋ฅผ ์ƒ์„ฑ
45
+ response = openai.ChatCompletion.create(
46
+ model="gpt-4", # GPT-4 ๋ชจ๋ธ์„ ์‚ฌ์šฉ
47
+ messages=[
48
+ {"role": "system", "content": "You are a helpful assistant that generates articles."},
49
+ {"role": "user", "content": prompt} # ์‚ฌ์šฉ์ž๊ฐ€ ์ œ๊ณตํ•œ ํ”„๋กฌํ”„ํŠธ
50
+ ],
51
  max_tokens=512, # ์ƒ์„ฑํ•  ํ…์ŠคํŠธ์˜ ์ตœ๋Œ€ ๊ธธ์ด
52
+ temperature=0.7 # ์ฐฝ์˜์„ฑ ์ •๋„
53
  )
54
+ return response['choices'][0]['message']['content'] # GPT์˜ ์‘๋‹ต ํ…์ŠคํŠธ ๋ฐ˜ํ™˜
55
  except Exception as e:
56
  return f"Error generating text: {e}"
57