File size: 1,705 Bytes
4381e7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfc1a13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import gradio as gr
import openai
import re  # import the regular expression module to parse the score

def evaluate_text(api_key, model, text_to_evaluate):
    # Setting the API key for OpenAI
    openai.api_key = api_key
    
    # Few-shot learning prompt template
    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.

1. "I am going to the park today." - Human written
    Evaluation: 5
    
2. "The quixotic endeavor synergizes cryptographic algorithms." - AI generated
    Evaluation: 90

3. "{}"
    Evaluation: """.format(text_to_evaluate)

    try:
        # Generate model's response
        response = openai.Completion.create(
            engine=model,
            prompt=prompt,
            max_tokens=10
        )
        
        raw_evaluation_score = response.choices[0].text.strip()
        
        # Parsing the evaluation score using regular expressions
        parsed_score = re.search(r"\d+", raw_evaluation_score)
        
        if parsed_score:
            return parsed_score.group()
        else:
            return "Score could not be parsed."

    except Exception as e:
        return str(e)

# Gradio UI
iface = gr.Interface(
    fn=evaluate_text,  
    inputs=[
        gr.inputs.Textbox(lines=1, label="Enter your OpenAI API Key", type="password"),
        gr.inputs.Dropdown(choices=["text-davinci-002", "text-ada-002"], label="Model"),
        gr.inputs.Textbox(lines=5, label="Text to Evaluate")
    ],
    outputs=gr.outputs.Textbox(label="AI Score out of 100"),  # Customized label here
    live=False
)

iface.launch()