Cartof commited on
Commit
ff45051
1 Parent(s): de57686

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -39
app.py CHANGED
@@ -7,48 +7,54 @@ css_file = os.path.join(os.path.dirname(file), "style.css")
7
  initial_prompt = "You are a helpful assistant."
8
 
9
  def parse_text(text):
10
- lines = text.split("\n")
11
- for i,line in enumerate(lines):
12
- if "" in line: items = line.split('`') if items[-1]: lines[i] = f'<pre><code class="{items[-1]}">' else: lines[i]= '<pre><code>' elif "" in line and "</code></pre>" not in line:
13
- lines[i] = "</code></pre>"
14
- elif "#" in line:
15
- lines[i] = f"<h1>{line.strip('#')}</h1>"
16
- elif "##" in line:
17
- lines[i] = f"<h2>{line.strip('##')}</h2>"
18
- elif "###" in line:
19
- lines[i] = f"<h3>{line.strip('###')}</h3>"
20
- return "\n".join(lines)
 
 
 
 
 
 
21
 
22
  def generate_response(prompt):
23
- # procesăm promptul
24
- prompt = parse_text(prompt)
25
- # configurăm OpenAI API
26
- openai.api_key = "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"
27
- model_engine = "davinci"
28
- prompt_length = len(prompt)
29
- max_tokens = 1024 - prompt_length
30
- temperature = 0.7
31
- #generăm răspunsul folosind OpenAI API
32
- response = openai.Completion.create(
33
- engine=model_engine,
34
- prompt=prompt,
35
- max_tokens=max_tokens,
36
- n=1,
37
- temperature=temperature,
38
- )
39
- message = response.choices[0].text.strip()
40
- # procesăm răspunsul generat
41
- message = parse_text(message)
42
- return message
43
 
44
- definim interfața Gradio
45
  iface = gr.Interface(
46
- fn=generate_response,
47
- inputs=gr.inputs.Textbox(lines=7, label="Input prompt"),
48
- outputs=gr.outputs.HTML(label="Model response"),
49
- title="OpenAI GPT-3 assistant",
50
- layout="vertical",
51
- width=800,
52
- css=css_file,
53
  )
54
  iface.launch()
 
7
  initial_prompt = "You are a helpful assistant."
8
 
9
  def parse_text(text):
10
+ lines = text.split("\n")
11
+ for i, line in enumerate(lines):
12
+ if "`" in line:
13
+ items = line.split('`')
14
+ if items[-1]:
15
+ lines[i] = f'<pre><code class="{items[-1]}">'
16
+ else:
17
+ lines[i] = '<pre><code>'
18
+ elif "`" in line and "</code></pre>" not in line:
19
+ lines[i] = "</code></pre>"
20
+ elif "#" in line:
21
+ lines[i] = f"<h1>{line.strip('#')}</h1>"
22
+ elif "##" in line:
23
+ lines[i] = f"<h2>{line.strip('##')}</h2>"
24
+ elif "###" in line:
25
+ lines[i] = f"<h3>{line.strip('###')}</h3>"
26
+ return "\n".join(lines)
27
 
28
  def generate_response(prompt):
29
+ # procesăm promptul
30
+ prompt = parse_text(prompt)
31
+ # configurăm OpenAI API
32
+ openai.api_key = "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"
33
+ model_engine = "davinci"
34
+ prompt_length = len(prompt)
35
+ max_tokens = 1024 - prompt_length
36
+ temperature = 0.7
37
+ #generăm răspunsul folosind OpenAI API
38
+ response = openai.Completion.create(
39
+ engine=model_engine,
40
+ prompt=prompt,
41
+ max_tokens=max_tokens,
42
+ n=1,
43
+ temperature=temperature,
44
+ )
45
+ message = response.choices[0].text.strip()
46
+ # procesăm răspunsul generat
47
+ message = parse_text(message)
48
+ return message
49
 
50
+ # definim interfața Gradio
51
  iface = gr.Interface(
52
+ fn=generate_response,
53
+ inputs=gr.inputs.Textbox(lines=7, label="Input prompt"),
54
+ outputs=gr.outputs.HTML(label="Model response"),
55
+ title="OpenAI GPT-3 assistant",
56
+ layout="vertical",
57
+ width=800,
58
+ css=css_file,
59
  )
60
  iface.launch()