Spaces:
Runtime error
Runtime error
Commit
·
4381e7b
1
Parent(s):
7618c51
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import re # import the regular expression module to parse the score
|
4 |
+
|
5 |
+
def evaluate_text(api_key, model, text_to_evaluate):
|
6 |
+
# Setting the API key for OpenAI
|
7 |
+
openai.api_key = api_key
|
8 |
+
|
9 |
+
# Few-shot learning prompt template
|
10 |
+
prompt = """Given below are some texts. Please evaluate how likely each text is AI-generated on a scale from 1-100, where 100 means highly likely and 1 means highly unlikely to be AI-generated.
|
11 |
+
|
12 |
+
1. "I am going to the park today." - Human written
|
13 |
+
Evaluation: 5
|
14 |
+
|
15 |
+
2. "The quixotic endeavor synergizes cryptographic algorithms." - AI generated
|
16 |
+
Evaluation: 90
|
17 |
+
|
18 |
+
3. "{}"
|
19 |
+
Evaluation: """.format(text_to_evaluate)
|
20 |
+
|
21 |
+
try:
|
22 |
+
# Generate model's response
|
23 |
+
response = openai.Completion.create(
|
24 |
+
engine=model,
|
25 |
+
prompt=prompt,
|
26 |
+
max_tokens=10
|
27 |
+
)
|
28 |
+
|
29 |
+
raw_evaluation_score = response.choices[0].text.strip()
|
30 |
+
|
31 |
+
# Parsing the evaluation score using regular expressions
|
32 |
+
parsed_score = re.search(r"\d+", raw_evaluation_score)
|
33 |
+
|
34 |
+
if parsed_score:
|
35 |
+
return parsed_score.group()
|
36 |
+
else:
|
37 |
+
return "Score could not be parsed."
|
38 |
+
|
39 |
+
except Exception as e:
|
40 |
+
return str(e)
|
41 |
+
|
42 |
+
# Gradio UI
|
43 |
+
iface = gr.Interface(
|
44 |
+
fn=evaluate_text,
|
45 |
+
inputs=[
|
46 |
+
gr.inputs.Textbox(lines=1, label="Enter your OpenAI API Key", type="password"),
|
47 |
+
gr.inputs.Dropdown(choices=["text-davinci-002", "text-ada-002"], label="Model"),
|
48 |
+
gr.inputs.Textbox(lines=5, label="Text to Evaluate")
|
49 |
+
],
|
50 |
+
outputs=gr.outputs.Textbox(label="AI Score out of 100"), # Customized label here
|
51 |
+
live=False
|
52 |
+
)
|
53 |
+
|
54 |
+
iface.launch()
|